Диалог подтверждения Android выдает nullpointerexception - PullRequest
0 голосов
/ 14 февраля 2012

Вот мой код для Confirm Dialogue, написанный в 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();

Это выдает nullpointerexception.Я действительно застрял в этой маленькой ошибке.Пожалуйста, помогите мне.

ОБНОВЛЕНИЕ : трассировка стека исключений Logcat:

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)

Ответы [ 3 ]

1 голос
/ 14 февраля 2012

заменить

getApplicationContext()

по

this

Конструктор должен быть создан с использованием контекста, который является действием, а не приложением.

0 голосов
/ 14 февраля 2012

вместо getApplicationContext () используйте classname.this. Это поможет вам

0 голосов
/ 14 февраля 2012

Используйте этот код:

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