0

Here is my code for Confirm Dialogue, written in onTouchListener:

new AlertDialog.Builder(getApplicationContext())
    .setTitle("Submit Quiz")
    .setMessage("Are you sure to submit the quiz")
    .setIcon(R.drawable.icon)
    .setPositiveButton("Yes",
        new OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                submitQuizConfirmed();
            }
        })
    .setNegativeButton("No",
        new OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
            }
        }
).show();

This throws nullpointerexception. I am really stuck at this little error. Please help me.

UPDATE: Logcat exception stacktrace:

02-14 13:06:07.023: WARN/WindowManager(59): Attempted to add window with non-application token WindowToken{44f0fbc8 token=null}.  Aborting.
02-14 13:06:07.023: DEBUG/AndroidRuntime(2036): Shutting down VM
02-14 13:06:07.023: WARN/dalvikvm(2036): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): FATAL EXCEPTION: main
02-14 13:06:07.053: ERROR/AndroidRuntime(2036): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.ViewRoot.setView(ViewRoot.java:509)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
0    2-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.app.Dialog.show(Dialog.java:241)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at uk.co.planetbeyond.service.Quiz$QuizQuestionsAdapter.submitQuizPressed(Quiz.java:146)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at uk.co.planetbeyond.service.Quiz$QuizQuestionsAdapter.moveToNextPage(Quiz.java:122)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at uk.co.planetbeyond.service.Quiz$MyGestureDetector.onFling(Quiz.java:86)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.GestureDetector.onTouchEvent(GestureDetector.java:568)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at uk.co.planetbeyond.service.Quiz$1.onTouch(Quiz.java:57)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.View.dispatchTouchEvent(View.java:3762)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.os.Looper.loop(Looper.java:123)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at java.lang.reflect.Method.invoke(Method.java:521)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 13:06:07.053: ERROR/AndroidRuntime(2036):     at dalvik.system.NativeStart.main(Native Method)
  • comment `submitQuizConfirmed();` and see if the problem still persists, otherwise there may be something wrong with this method – waqaslam Feb 14 '12 at 13:20
  • @Waqas problem still persists. Please see the logcat I have added –  Feb 14 '12 at 13:25
  • @Ajinkya yesd sure sir, I have added Logcat stacktrace, please have a look –  Feb 14 '12 at 13:27
  • @Downvoter : Y downvote? I never mind, but want to know the reason please –  Feb 14 '12 at 13:28
  • 1
    replace `getApplicationContext()` by `getBaseContext()` and try – waqaslam Feb 14 '12 at 13:30
  • 1
    http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window – Ajinkya Feb 14 '12 at 13:32

3 Answers3

1

replace

getApplicationContext()

by

this

A builder must be created using a context that is an activity, not an application.

njzk2
  • 38,969
  • 7
  • 69
  • 107
0

Use this code:

AlertDialog ad = new AlertDialog.Builder(MainStart.this).create();
ad.setTitle("Error");
ad.setMessage("plz select the username and password");
ad.setButton("ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

    }
});
ad.show();
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Nitesh Khosla
  • 875
  • 8
  • 20
0

instead of getApplicationContext() use classname.this. It will help you