2011-08-17 6 views
0

Possible Duplicate:
Android 1.6: "android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application"안드로이드에 윈도우 오류를 추가 할 수 없습니다

내가 다른 일을 시도했습니다,하지만 난 여전히 같은 오류 계속 :이 줄에

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

답변

-2

나는 이것이 스레드에서 실행 중이기 때문이라고 생각합니다. alertDialog.show();은 UI 스레드에서 실행되어야합니다. 대신 AsyncTask을 사용해보세요.

편집 : 내 나쁜, 나는 코드를주의 깊게 읽지 않았다.

0

alertDialog를 만들 때 사용하는 컨텍스트가 지원하지 않기 때문입니다. 따라서 mContext 대신 getParent() 또는 getApplicationContext ()를 시도하십시오. 그럴 수도 있습니다.

+0

활동이 alertDialog를 지원하지 않는 이유는 무엇입니까? 나는 항상 대화 상자를 아무런 문제없이 구축한다. 그 스레드를 참조하십시오 : http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window –

+0

당신의 활동이 대화를 지원하지 않는다고 말하는 것이 아닙니다. 활동 그룹 또는 어댑터 클래스를 사용하는 경우에는이 문제가 발생합니다. 이 시간에는 컨텍스트를 제대로 확인해야 할 수도 있습니다. 나는 이것을 여러 번 해왔다. 이것은 단지 제 제안이었습니다. 그리고 그는 alertDialog.show()를 사용하지 말 것을 제안했습니다. 스레드에서. 그는 핸들러에서 사용하고 있는데, 이것은 쓰레드에 상관없이 정확합니다. –

+0

어이 걱정하지 마라. 나는 단지 토론하고있다. 아무도 잘못한 것은 아닙니다. 제 대답을 위해서, 나는 그의 코드에 충분한주의를 기울이지 않았습니다. 내 나쁜 :) –

관련 문제