1


Me used the following code to create a AlertDialog.

 AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); 

But it shows error on alert.show()
The error i got is

02-03 11:36:43.204: WARN/dalvikvm(452): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 02-03 11:36:43.214: ERROR/AndroidRuntime(452): Uncaught handler: thread main exiting due to uncaught exception 02-03 11:36:43.234: ERROR/AndroidRuntime(452): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 02-03 11:36:43.234: ERROR/AndroidRuntime(452): at android.view.ViewRoot.setView(ViewRoot.java:472) 02-03 11:36:43.234: ERROR/AndroidRuntime(452): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 

this class is an activity

public class HomeTabActivity extends Activity 

This is HomeTabActivity is one the groupActivity since me using each tap as an activity. I called this activity like this

 View view = getLocalActivityManager().startActivity("hometab", new Intent(this,HomeTabActivity.class) .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView(); replaceView(view); 

What did i miss. Thanks in advance

3 Answers 3

6

First line is wrong. When calling it from Activity it should be

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
Sign up to request clarification or add additional context in comments.

1 Comment

is true answer!
4

Try to use

 AlertDialog.Builder builder = new AlertDialog.Builder(getParent()); 

Comments

1

Have you tried using Activity methods onCreateDialog(int id) and call it with showDialog(id)? Here is good resource about dialogs in android.

2 Comments

To add more details using onCreateDialog() instead of dialog.show() is better because it does some magic that keeps the dialog on the screen even when the activity is recreated (such as when rotating the screen). But it has nothing to do with your error (see Zelimir answer for that)
You are right. I just wanted to help him solving current issue. Managing AlertDialog magic related to screen rotation is just a metter of one boolean saved in onSaveInstanceState() and recovered in onCreate().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.