2011-08-17 58 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

相關問題