主要活动上的getapplicatiocontext或getbasecontext

时间:2014-03-18 14:39:17

标签: android

当我打电话给它时,它无法正常工作

 AlertDialog alertDialog = new AlertDialog.Builder(getBaseContext()).create();
 // Setting Dialog Title
 alertDialog.setTitle(e.getMessage());
 alertDialog.show();

MainActivity中,如何让它发挥作用?

堆栈跟踪是:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not 
        for an application 03-18 16:38:07.505: 
 E/AndroidRuntime(13152): at android.view.ViewRootImpl.setView(ViewRootImpl.java:540) 03-18 16:38:07.505: 
 E/AndroidRuntime(13152): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) –

1 个答案:

答案 0 :(得分:2)

如果您在扩展Activity的课程中,可以使用

  AlertDialog alertDialog = new AlertDialog.Builder(this).create();
   // Setting Dialog Title
  alertDialog.setTitle(e.getMessage());
  if(!mActivity.this.isFinishing()) {
        alertDialog.show();
  }