質問

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

my custom 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();                 
    }          

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

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.

enter image description here

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.

役に立ちましたか?

解決

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);

Both should work.

他のヒント

replace

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

with

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

Dialog requires a Context reference whose window token is not null such as Activity reference...

Make the following changes

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

Either you need Context or currentActivity. Then sure it will work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top