1

I'm having difficulty when making an alert dialog, the code for it is here:

public void setp2(View v){
        p2 = (TextView)findViewById(R.id.p2);
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
        alertDialogBuilder.setTitle("Please enter player 2 name");
        alertDialogBuilder.setMessage("MESSAGE");
        alertDialogBuilder.setCancelable(false);
        alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int id) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();   

}

Whenever I run this it crashes the logcat looks like:

04-14 06:09:36.282: E/Trace(25945): error opening trace file: No such file or directory (2)
04-14 06:09:37.822: E/AndroidRuntime(25945): FATAL EXCEPTION: main
04-14 06:09:37.822: E/AndroidRuntime(25945): java.lang.IllegalStateException: Could not execute method of the activity
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.View$1.onClick(View.java:3595)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.View.performClick(View.java:4088)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.View$PerformClick.run(View.java:16984)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.os.Handler.handleCallback(Handler.java:615)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.os.Looper.loop(Looper.java:137)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at java.lang.reflect.Method.invoke(Method.java:511)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at dalvik.system.NativeStart.main(Native Method)
04-14 06:09:37.822: E/AndroidRuntime(25945): Caused by: java.lang.reflect.InvocationTargetException
04-14 06:09:37.822: E/AndroidRuntime(25945):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at java.lang.reflect.Method.invoke(Method.java:511)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.View$1.onClick(View.java:3590)
04-14 06:09:37.822: E/AndroidRuntime(25945):    ... 11 more
04-14 06:09:37.822: E/AndroidRuntime(25945): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at android.app.Dialog.show(Dialog.java:277)
04-14 06:09:37.822: E/AndroidRuntime(25945):    at uk.co.mikecoombes.oxo.MainActivity.setp2(MainActivity.java:46)
04-14 06:09:37.822: E/AndroidRuntime(25945):    ... 14 more

I've looked through it several times and can't seem to find where it's going wrong, was wondering if anyone could help out?

edwin
  • 7,985
  • 10
  • 51
  • 82
Coombes
  • 203
  • 1
  • 10
  • what is in AllComediansActivity.java files 93 line? – stinepike Apr 14 '13 at 05:13
  • I don't see `setp2()` in your stack trace, the problem is in your AsynTask's `doInBackground()`, post this method and mark line 93. – Sam Apr 14 '13 at 05:17
  • Hmmm how odd my Logcat doesn't seem to have saved over the previous log I saved, should be sorted now – Coombes Apr 14 '13 at 05:17

1 Answers1

5
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

It appears the Context is bad, sometimes getApplicationContext() returns odd results... Try changing this:

new AlertDialog.Builder(getApplicationContext());

To:

new AlertDialog.Builder(MainActivity.this);

Check the answer by commonsware in the link

When to call activity context OR application context?

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • 2
    i have deleted my answer since the answer is the same. Copied the link and posted the same in your answer. Thought the link might help. – Raghunandan Apr 14 '13 at 05:36
  • Thanks RaghuNandan, you respected SO's rules :) – AAnkit Apr 14 '13 at 05:37
  • @Raghunandan Ok, that is a good link. I upvoted one of your other answers to say thanks. (Your answer wasn't wrong, it's just silly to have the same answer duplicated.) – Sam Apr 14 '13 at 05:42