0

次のコードを実行すると例外が発生します。

public class LoginActivity extends Activity {
/** Called when the activity is first created. */
Button btnSend, btnDelete;
EditText edtUsername, edtPassword;
TextView tvUsername, tvPassword;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initializeProperties();

    btnGonder.setOnClickListener(new OnClickListener() { public void onClick(View view) {           
        login("aaa@bbb.com", "123456");
    }});
    btnSil.setOnClickListener(new OnClickListener() { public void onClick(View view) {
        clearView();
    }});
}

private void initializeProperties() {
    btnSend = (Button) findViewById(R.id.bSend);
    btnDelete = (Button) findViewById(R.id.bDelete);
    tvUsername = (TextView) findViewById(R.id.tvUsername);
    edtUsername = (EditText) findViewById(R.id.etUsername);
    tvPassword = (TextView) findViewById(R.id.tvPassword);
    edtPassword = (EditText) findViewById(R.id.etPassword);
}

private static final String readInputStream(java.io.InputStream stream) throws IOException {
    java.io.InputStreamReader streamReader = new java.io.InputStreamReader(stream);
    java.io.BufferedReader bufferedReader = new java.io.BufferedReader(streamReader);
    String line, redText = "";
    while ((line = bufferedReader.readLine()) != null) {
        redText += line;
    }
    bufferedReader.close();
    return redText;
}

private void clearView() {
    edtPassword.setText("");
}

private void login(String username, String password) {

    HttpPost httpPost = new HttpPost("http://192.168.1.1/api/login");

    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getApplicationContext());
    alertBuilder.setCancelable(true);

    //HttpResponse response = null;
    try {
        // Add your data
        httpPost.setEntity(new StringEntity(
            String.format("{ 'Email': '%s', 'Password': '%s' }", username, password)
        ));
        // Set Headers
        httpPost.setHeader("Content-Type", "application/json; charset=utf-8");
        // Execute HTTP Post Request
        //response = new AsyncLoginHandler(alertBuilder).execute(httpPost).get();
        new AsyncLoginHandler(alertBuilder).execute(httpPost);

    } catch (UnsupportedEncodingException uee) {
        alertBuilder.setMessage(uee.getMessage());
    }
}

class AsyncLoginHandler extends android.os.AsyncTask<HttpPost, Void, HttpResponse> {
    AlertDialog.Builder alertBuilder;

    AsyncLoginHandler(AlertDialog.Builder alertBuilder) {
        super();
        this.alertBuilder = alertBuilder;
    }

    @Override
    protected HttpResponse doInBackground(HttpPost... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = null;
        for (int i = 0; i < params.length; i++) {
            try {
                httpResponse = httpClient.execute(params[i]);
            } catch (ClientProtocolException cpe) {
                alertBuilder.setMessage(cpe.getMessage());
            } catch (IOException ioe) {
                alertBuilder.setMessage(ioe.getMessage());
            } catch (Exception e) {
                alertBuilder.setMessage(e.getMessage());
            }
        }
        return httpResponse;
    }

    @Override
    protected void onPostExecute(HttpResponse response) {
        if (response != null) {
            try {
                java.io.InputStream stream = response.getEntity().getContent();
                String result = readInputStream(stream);
                alertBuilder.setMessage(result);
            } catch (IllegalStateException ise) {
                alertBuilder.setMessage(ise.getMessage());
            } catch (IOException ioe) {
                alertBuilder.setMessage(ioe.getMessage());
            }
        }

        super.onPostExecute(response);

        alertBuilder.create().show();
    }

}

}

そうですね... AsyncTaskの Android のドキュメント ページ(「4 つのステップ」というタイトルを参照してください) には、UI スレッドで onPostExecute が呼び出されると記載されています。ただし、「 alertBuilder.create().show() ;」の後に次のエラーが発生します。

 - 10-04 06:36:25.607: W/dalvikvm(711): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
 - 10-04 06:36:25.657: E/AndroidRuntime(711): FATAL EXCEPTION: main
 - 10-04 06:36:25.657: E/AndroidRuntime(711): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.view.ViewRootImpl.setView(ViewRootImpl.java:517)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.app.Dialog.show(Dialog.java:278)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at org.gdgankara.com.LoginActivity$AsyncLoginHandler.onPostExecute(LoginActivity.java:139)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at org.gdgankara.com.LoginActivity$AsyncLoginHandler.onPostExecute(LoginActivity.java:1)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.os.AsyncTask.finish(AsyncTask.java:602)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.os.AsyncTask.access$600(AsyncTask.java:156)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.os.Handler.dispatchMessage(Handler.java:99)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.os.Looper.loop(Looper.java:137)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at android.app.ActivityThread.main(ActivityThread.java:4424)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at java.lang.reflect.Method.invokeNative(Native Method)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at java.lang.reflect.Method.invoke(Method.java:511)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 - 10-04 06:36:25.657: E/AndroidRuntime(711):   at dalvik.system.NativeStart.main(Native Method)

誰でも助けることができますか?

4

1 に答える 1

-1

doInBackground メソッドは、メイン スレッドのビューに触れることはできません。doInBackground メソッドから警告ダイアログのメッセージを設定できないことを意味します。doInBackground 内のキャッチから set 3 set メッセージを削除して、アプリケーションを実行してみてください。

于 2012-10-04T07:38:21.503 に答える