AsyncTask中的AlertDialog.Builder抛出异常

时间:2014-11-26 17:35:44

标签: android android-asynctask android-alertdialog

我正在尝试在AsyncTask中使用AlertDialog.Builder,这种抛出是如此奇怪的异常,我无法解决它。我想在onPostExecute

中打开此AlertDialog

问题在于:

11-26 15:27:35.805: E/AndroidRuntime(6286): FATAL EXCEPTION: main
11-26 15:27:35.805: E/AndroidRuntime(6286): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:800)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.app.Dialog.show(Dialog.java:287)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at br.com.mypackage.myapp.act.CadastrarView$2.onPostExecute(CadastrarView.java:190)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at br.com.mypackage.myapp.act.CadastrarView$2.onPostExecute(CadastrarView.java:1)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.os.AsyncTask.finish(AsyncTask.java:631)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.os.Looper.loop(Looper.java:176)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at android.app.ActivityThread.main(ActivityThread.java:5419)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at java.lang.reflect.Method.invokeNative(Native Method)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at java.lang.reflect.Method.invoke(Method.java:525)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
11-26 15:27:35.805: E/AndroidRuntime(6286):     at dalvik.system.NativeStart.main(Native Method)

我在这里尝试

new AsyncTask<String, Void, String>() {
    @Override
            protected String doInBackground(String... params) {
        add = new UsuarioDAO().insert(usuario, fotoPerfil);                                 
        return "executed";
    }

    @Override
            protected void onPostExecute(String result) {
        progress.dismiss(); 
        if(add){
            AlertDialog.Builder alert = new AlertDialog.Builder(getApplicationContext());
            alert.setTitle("Title");
            alert.setMessage("Obrigado por se cadastrar\n"
                             + "Uma mensagem de confirmação foi enviada para seu email\n");
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {                           
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            startActivity(new Intent(getApplicationContext(), LoginView.class));
                                        }
                                    });     
            AlertDialog dialog = alert.create();
            dialog.show();
        }else{
            Toast.makeText(CadastrarView.this, "Erro encontrado tentando adicionar conta", Toast.LENGTH_SHORT).show();
        }   
    }
}.execute("");

1 个答案:

答案 0 :(得分:1)

getApplicationContext()替换为thisWhateverYourActivityIsCalled.this

当您知道正是为什么 使用getApplicationContext()时,才使用getApplicationContext()。请参阅Dave Smith的this blog post以了解有关不同Context对象的角色的更多信息。