AlertDialog.show () makes my application crash

here is my problem:

I am trying to show AlertDialog, but I cannot do it.

Here is my code:

tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { final EditText input = new EditText(c); AlertDialog.Builder adb = new AlertDialog.Builder(c); adb.setTitle(lb) .setMessage("Test") .setView(input) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //tv.setText(input.getEditableText().toString()); Toast.makeText(c, input.getEditableText().toString(), Toast.LENGTH_LONG).show(); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dialog.cancel(); } }).show(); } }); 

I want to show this AlertDialog when the user clicks on the label, and then changes the label value with the edit text when the user clicks OK.

But when it comes to showing the dialog when clicked, it will work.

 07-18 16:04:59.240: E/AndroidRuntime(10503): FATAL EXCEPTION: main 07-18 16:04:59.240: E/AndroidRuntime(10503): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.view.ViewRootImpl.setView(ViewRootImpl.java:710) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.app.Dialog.show(Dialog.java:277) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.app.AlertDialog$Builder.show(AlertDialog.java:932) 07-18 16:04:59.240: E/AndroidRuntime(10503): at com.technicachat.webdatadomo.Consignes$2$1.run(Consignes.java:114) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.app.Activity.runOnUiThread(Activity.java:4784) 07-18 16:04:59.240: E/AndroidRuntime(10503): at com.technicachat.webdatadomo.Consignes$2.onClick(Consignes.java:90) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.view.View.performClick(View.java:4211) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.view.View$PerformClick.run(View.java:17267) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.os.Handler.handleCallback(Handler.java:615) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.os.Handler.dispatchMessage(Handler.java:92) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.os.Looper.loop(Looper.java:137) 07-18 16:04:59.240: E/AndroidRuntime(10503): at android.app.ActivityThread.main(ActivityThread.java:4898) 07-18 16:04:59.240: E/AndroidRuntime(10503): at java.lang.reflect.Method.invokeNative(Native Method) 07-18 16:04:59.240: E/AndroidRuntime(10503): at java.lang.reflect.Method.invoke(Method.java:511) 07-18 16:04:59.240: E/AndroidRuntime(10503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 07-18 16:04:59.240: E/AndroidRuntime(10503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 07-18 16:04:59.240: E/AndroidRuntime(10503): at dalvik.system.NativeStart.main(Native Method) 

Fewer lines before I received this message:

 07-18 16:04:56.645: I/Choreographer(10503): Skipped 32 frames! The application may be doing too much work on its main thread. 

I know what that means, but I'm just showing a dialogue, it doesn't work that much!

Hope you guys can help me!

Bye

+6
source share
6 answers

Your c variable should be YourActivity.this instead of getApplicationContext()

+26
source

i ran into this problem from 1.2 days, but I solved it by changing

final Dialog dialogView = new dialog (getApplicationContext ());

to

final Dialog dialogView = new dialog (Leave_Notification_Activity.this);

you should not use getApplicationContext () and not pass YourActivity.this to solve this problem.

+2
source

There are a few things you need to consider.

  • You customize the alert dialog, for example, position the button (s) and set layout options, such as fields.

    If you do, using the V7 Support Alert dialog box will solve the problem. Make sure you import the v7 support library into your project.

    Change android.app.AlertDialog.Builder to android.support.v7.app.AlertDialog.Builder

  • You create an Alert dialog box in an Activity or subclass of Activity or inside a fragment Use

    Always pass activity context and NOT baseContext or applicationContext

    passing the wrong context (e.g. applicationContext or baseContext) will result in a WindowManager-BadToken exception

In activity ...

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

In the activity subclass ...

 AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this); 

In the fragment ...

 AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity()); // Activity inherits from Context and hence will work. 

What I found is that the heavy settings in Alert Dialog work well with the AlertDialog v7 support class.

In my case, I had to center the Alert Dialog buttons and set the left and right margins if there was more than one button. Changing import support in v7 solved the problem.

Hope this helps.

+1
source

try this toast.

 Toast.makeText(getApplicationContext(), input.getEditableText().toString(), Toast.LENGTH_LONG).show(); 
0
source

You cannot display anything without creating an object of class AlertDialog .

 AlertDialog ad=adb.create(); ad.show(); 

Now he will check it once.

0
source

If you used Kotlin, you would use this@YourActivity instead of applicationContext .

0
source

Source: https://habr.com/ru/post/1492104/


All Articles