Labeeb Panampullan
Labeeb Panampullan

Reputation: 34823

AlertDialog error in android


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

Upvotes: 1

Views: 14312

Answers (3)

Tanmay Mandal
Tanmay Mandal

Reputation: 40168

Try to use

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

Upvotes: 4

Zelimir
Zelimir

Reputation: 11028

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

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

Upvotes: 6

svidnas
svidnas

Reputation: 581

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

Upvotes: 1

Related Questions