Question

The logcat complains about something with the alertDialog.show(). when i comment out the below posted method, the app works, otherwise, it crashes. Please let me know where the error is.

Java Code:

private void takePictureOrDoNot() {
        // TODO Auto-generated method stub
        AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(getApplicationContext());
        mAlertDialogBuilder.setTitle("Picture For Your Location.");
        mAlertDialogBuilder
             .setMessage("Do You Want To Take a Picture Of Your Location?")
             .setCancelable(true)
             .setPositiveButton("YES", mDialogAlertPositiveButtonSetListener)
             .setNegativeButton("NO", mDialogAlertNegativeButtonSetListener);
        AlertDialog mAlertDialog = mAlertDialogBuilder.create(); 
        mAlertDialog.show(); 
    }

Logcat:

05-13 22:05:32.766: E/AndroidRuntime(26463): FATAL EXCEPTION: main
05-13 22:05:32.766: E/AndroidRuntime(26463): 
android.view.WindowManager$BadTokenException: Unable to add window -- token null is  
not for an application
05-13 22:05:32.766: E/AndroidRuntime(26463):    at 
android.view.ViewRootImpl.setView(ViewRootImpl.java:802)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at  
android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at 
android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at 
android.app.Dialog.show(Dialog.java:287)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at  
com.example.meetingpointlocator_03.AddNewLocationActivity$3.takePictureOrDoNot(AddNewLocationActivity.java:185)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at com.example.meetingpointlocator_03.AddNewLocationActivity$3.onKey(AddNewLocationActivity.java:150)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at android.view.View.dispatchKeyEvent(View.java:7672)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1485)
05-13 22:05:32.766: E/AndroidRuntime(26463):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2293)
Was it helpful?

Solution

Use

AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(ActivityName.this);

instead of getApplicationContext().

And Read Commonsware answer @

When to call activity context OR application context?

OTHER TIPS

In an Activity

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

In a Fragment

AlertDialog.Builder mAlertDialogBuilder = new AlertDialog.Builder(getActivity());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top