自定义对话框中的java.lang.IllegalStateException [英] java.lang.IllegalStateException in Custom Dialog

查看:82
本文介绍了自定义对话框中的java.lang.IllegalStateException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to Android and I'm working on custom alert dialog

我想在单击编辑按钮时打开另一个对话框,并且代码在下面

I want to open a another dialog-box on click of edit button and code is below

 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输出如下所示

But I am getting an exception and my log cat output is as follow

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,但我没有得到什么结果代码错误。任何帮助都非常感谢。

Even my redcross button is working fine and I've used same code for showing AlertDialog with TextView many times, but I did not get what is going wrong with code. Any help is really appreciated. Thanks in advance.

现在我得到了我想要的东西,但是编辑和保存两者都重叠在一起。我想隐藏编辑(蓝色按钮)并清楚地显示保存按钮。

Now i got whatever i want , but edit and save both are coming both with overlap . I want to hide edit ( blue button )and make visible save button clearly.

推荐答案

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

If you read the documentation at getApplicationContext() you find that you should only use this if you need a Context whose lifecycle is separate from the current context. This doesn't apply in your examples and use of this object as an object of type className.I think here you can use

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

OR

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

两者都应该起作用。

这篇关于自定义对话框中的java.lang.IllegalStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆