android中的警告对话框显示错误

时间:2014-04-17 10:12:33

标签: android

你好朋友我是android的新手,我想在我的应用程序中显示一个警告框。我做了这个

Button butalert=(Button)this.findViewById(R.id.button1alert);  
butalert.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
           // TODO Auto-generated method stub
           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
           alertDialogBuilder.setMessage("vaibhav");
           alertDialogBuilder.create();
           alertDialogBuilder.show();
        }           
});

但是我收到这样的错误

04-17 15:36:13.556: E/AndroidRuntime(2630): FATAL EXCEPTION: main
04-17 15:36:13.556: E/AndroidRuntime(2630): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@410246f8 is not valid; is your activity running?
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.view.Window$LocalWindowManager.addView(Window.java:547)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.app.Dialog.show(Dialog.java:277)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at com.kaldarshika.FisrtActivity$5.onClick(FisrtActivity.java:473)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.view.View.performClick(View.java:4084)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.view.View$PerformClick.run(View.java:16966)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.os.Handler.handleCallback(Handler.java:615)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.os.Looper.loop(Looper.java:137)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at java.lang.reflect.Method.invokeNative(Native Method)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at java.lang.reflect.Method.invoke(Method.java:511)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-17 15:36:13.556: E/AndroidRuntime(2630):     at dalvik.system.NativeStart.main(Native Method)

请帮我解决这个问题

3 个答案:

答案 0 :(得分:0)

尝试替换

alertDialogBuilder.create();
alertDialogBuilder.show();

alertDialogBuilder.create().show();

答案 1 :(得分:0)

context更改为YourActivity.this

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivity.this);
 alertDialogBuilder.setMessage("vaibhav");
 alertDialogBuilder.create().show();

答案 2 :(得分:0)

context AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); 正确设置?

如果是这样,您可以尝试:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context); alertDialogBuilder.setMessage("vaibhav"); AlertDialog aDialog = alertDialogBuilder.create(); aDialog.show();