AlertDialog.show() makes my app crash

here's my problem :

I'm trying to show a AlertDialog, but i can't seem to be able to do it.

Here's 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 user click on a label, then change the label value with the edit text one when user press OK.

But when it comes to show the dialog on click, it crashes.

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 got 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 it means, but i'm just showing a dialog, it's not this much work !

I hope you guys can help me !

Bye

Answers


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


i was facing this issue from 1,2 days but i solved this by changing

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

to

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

you should not use getApplicationContext() instead pass YourActivity.this to solve this issue.


There are a couple of things you need to consider.

  1. You are customizing the Alert Dialog such as positioning the button(s) and setting Layout Parameters such as Margins.

    If you are doing this, using the V7 Support's Alert Dialog will resolve the problem. Make sure you have imported the v7 support library in your project.

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

  2. You are creating the Alert Dialog in an Activity or a sub class of the Activity or inside a Fragment Use

    Always pass the Activity context and NOT baseContext or applicationContext

    passing the wrong context (such as the applicationContext or the baseContext) will result in a WindowManager-BadToken exception

In the activity...

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

In the activity's sub class...

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

In a Fragment...

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

What I have found is that, heavy customizations to the Alert Dialog works well with the v7 Support AlertDialog class.

In my case, I had to center the Alert Dialog's buttons and set left and right margins, if there was more than a single button. Changing the import to v7 support resolved the issue.

Hope this helps.


try this one toast.

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

You cannot display anything with out creating AlertDialog class object.

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

Now it will works check it once.


If you were using Kotlin, then you'd use this@YourActivity instead of applicationContext.


Need Your Help

What is the new way of checking "callable" methods in python 3.x?

python python-3.x introspection

I was studying introspection in Python, and as I was getting through basic examples, I found out that the callable built-in function is no longer available in Python 3.1.

c# itextsharp PDF creation with watermark on each page

c# pdf itextsharp

I am trying to programmatically create a number of PDF documents with a watermark on each page using itextsharp (a C# port of Java's itext).