2011-08-17 83 views
0

Possible Duplicate:
Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application"无法添加窗口错误的Android

我已经尝试不同的东西,但我仍然保持了同样的错误:

android.view.WindowManager$BadTokenException: Unable to add window 

在这一行:

alertDialog.show() ;

你能看看代码吗?

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.splashscreen); 

    Context mContext = this; 
    alertDialog = new AlertDialog.Builder(mContext).create(); 

    LoadData(); 
} 

public void LoadData() 
{ 
    Thread t1 = new Thread(this); 
t1.start(); 
} 

private Handler handler = new Handler() 
{ 
@Override 
    public void handleMessage(Message msg) 
    {    
     if(!rssItems.isEmpty()) 
     { 
      switch (msg.what) { 
      case STOPSPLASH: 
       //remove SplashScreen from view 
       //splash.setVisibility(View.GONE); 
       Intent intent = new Intent(
       "news.displayNews"); 
       intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       NewsDisplayer.rssItems.clear(); 
       NewsDisplayer.rssItems.addAll(rssItems); 

       startActivity(intent); 
       Close(); 
       break; 
      } 
     } 
     else 
     { 
      alertDialog.setCancelable(false); // This blocks the 'BACK' button 
      alertDialog.setMessage("No connection."); 
      alertDialog.setTitle("Error..."); 
      alertDialog.setButton("Again", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss();  
        LoadData(); 
       } 
      }); 
      alertDialog.setButton2("Close", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss();  
        System.exit(0); 
       } 
      }); 
      alertDialog.show(); 
     } 
    } 
}; 
+0

这个问题是否提供任何方向http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window? – Sampson 2011-08-17 13:46:49

回答

-2

我认为这是因为你在一个线程中运行它。必须在UI线程上执行alertDialog.show();。尝试使用AsyncTask来代替。

编辑:我的坏,我没有仔细阅读代码。

0

这是因为您用于创建alertDialog的上下文不支持它。因此,而不是mContext,请尝试getParent()getApplicationContext()。这可能会起作用。

+0

活动如何不支持alertDialog?我总是用这种方式构建我的对话框,没有任何问题。看到这个线程︰http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window – 2011-08-17 12:34:38

相关问题