【问题标题】:java.lang.IllegalStateException in Custom Dialog自定义对话框中的 java.lang.IllegalStateException
【发布时间】:2014-01-23 09:24:03
【问题描述】:

我是 Android 新手,我正在开发自定义警报对话框

我想在点击编辑按钮时打开另一个对话框,代码如下

 if (v.getId() == R.id.edt_order) {                                  
        System.out.println(" edit buton click");                    
        System.out.println("Click my Order");
            System.out.println(" edit clickkkkkkkkkkkkkk");
         LayoutInflater li = LayoutInflater.from(getApplicationContext());
         View promptsView = li.inflate(R.layout.prompts, null);

         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                 getApplicationContext());

         // set prompts.xml to alertdialog builder  
         alertDialogBuilder.setView(promptsView);    

         final EditText userInput = (EditText) promptsView
         .findViewById(R.id.editTextDialogUserInput); 

         // set dialog message
         alertDialogBuilder
         .setCancelable(false)
         .setPositiveButton("OK",
                 new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
                 // get user input and set it to result 
                 // edit text
                 //    result.setText(userInput.getText());

                 System.out.println("Click ok");    
                // insertData(userInput.getText().toString().trim());
                 Toast.makeText(getApplicationContext(), "Category added", 5000).show();
                 // loadSpinnerData();
             }
         })
         .setNegativeButton("Cancel",
                 new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog,int id) {
                 dialog.cancel();
             }
         }); 

         // create alert dialog   
          alertDialog = alertDialogBuilder.create();

         // show it
         alertDialog.show();                 
    }          

但是我遇到了一个异常,我的 log cat 输出如下

01-23 14:46:57.438: D/AndroidRuntime(660): Shutting down VM
01-23 14:46:57.448: W/dalvikvm(660): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-23 14:46:57.558: D/dalvikvm(660): GC_FOR_MALLOC freed 3899 objects / 202144 bytes in 99ms
01-23 14:46:57.568: E/AndroidRuntime(660): FATAL EXCEPTION: main
01-23 14:46:57.568: E/AndroidRuntime(660): java.lang.IllegalStateException: Could not execute method of the activity
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.view.View$1.onClick(View.java:2072)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.view.View.performClick(View.java:2408)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.view.View$PerformClick.run(View.java:8816)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.os.Handler.handleCallback(Handler.java:587)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.os.Looper.loop(Looper.java:123)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-23 14:46:57.568: E/AndroidRuntime(660):  at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:46:57.568: E/AndroidRuntime(660):  at java.lang.reflect.Method.invoke(Method.java:521)
01-23 14:46:57.568: E/AndroidRuntime(660):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-23 14:46:57.568: E/AndroidRuntime(660):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-23 14:46:57.568: E/AndroidRuntime(660):  at dalvik.system.NativeStart.main(Native Method)
01-23 14:46:57.568: E/AndroidRuntime(660): Caused by: java.lang.reflect.InvocationTargetException
01-23 14:46:57.568: E/AndroidRuntime(660):  at com.example.demoekot.MainScreen.clickHandler(MainScreen.java:524)
01-23 14:46:57.568: E/AndroidRuntime(660):  at java.lang.reflect.Method.invokeNative(Native Method)
01-23 14:46:57.568: E/AndroidRuntime(660):  at java.lang.reflect.Method.invoke(Method.java:521)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.view.View$1.onClick(View.java:2067)
01-23 14:46:57.568: E/AndroidRuntime(660):  ... 11 more
01-23 14:46:57.568: E/AndroidRuntime(660): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.view.ViewRoot.setView(ViewRoot.java:509)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
01-23 14:46:57.568: E/AndroidRuntime(660):  at android.app.Dialog.show(Dialog.java:241)
01-23 14:46:57.568: E/AndroidRuntime(660):  ... 15 more

即使我的红叉按钮工作正常,我也多次使用相同的代码来显示带有 TextView 的 AlertDialog,但我没有发现代码出了什么问题。非常感谢任何帮助。提前致谢。

现在我得到了我想要的任何东西,但是编辑和保存都带有重叠。我想隐藏编辑(蓝色按钮)并使保存按钮清晰可见。

【问题讨论】:

标签: android exception android-alertdialog illegalstateexception


【解决方案1】:

如果您阅读getApplicationContext() 的文档,您会发现只有在需要生命周期与当前上下文分开的Context 时才应该使用它。这不适用于您的示例和使用 this 对象作为 className 类型的对象。我认为您可以在这里使用

  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ClassNAme.this);

两者都应该工作。

【讨论】:

    【解决方案2】:

    替换

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
    

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivityName.this);
    

    Dialog 需要一个Context 引用,其窗口令牌不是null,例如Activity 引用...

    【讨论】:

    • ..我想做另一件事,我希望在单击编辑按钮时不可见相同的编辑。
    • @PriyaSingh 在alertDialog.show(); 语句之后添加此代码v.setVisibility(View.INVISIBLE)...
    • 我遇到异常
    • @PriyaSingh 哪个异常?
    • System.out.println("编辑按钮点击"); System.out.println("点击我的订单"); System.out.println("编辑点击kkkkkkkkkkkkk"); img_btn_edit_order_var.setVisibility(ImageView.INVISIBLE);
    【解决方案3】:

    进行以下更改

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    

    【讨论】:

      【解决方案4】:

      您需要 Context 或 currentActivity。然后确定它会起作用。

      【讨论】:

        猜你喜欢
        • 2014-07-29
        • 1970-01-01
        • 2015-10-16
        • 2011-10-18
        • 2013-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-30
        相关资源
        最近更新 更多