开发者

AlertDialog error in android

开发者 https://www.devze.com 2023-02-08 01:27 出处:网络
Me used the following code to cre开发者_如何学Cate a AlertDialog. AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

Me used the following code to cre开发者_如何学Cate 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


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

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


Try to use

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消