2011-08-14 48 views
0

所以基本上我在我的TabHost中使用TabGroupActivity,所以我可以在一個選項卡內切換不同的活動。兩天之前,我現在運行的代碼是好的,沒有任何exceptions.But在當時我翻我改變與不同的所有視圖activities.So我的情況tab.Now裏面的觀點是這樣的事情:Android ALerDialog - 無法添加窗口異常

Main tab Activity -> opens Second activity -> which opens the third activity ,where the AlertDialog is. 

我正在使用的代碼是:

Button deactivate = (Button) findViewById(R.id.deactivate); 
     deactivate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       new AlertDialog.Builder(getParent()) 
       .setTitle("Warning") 
       .setMessage("The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection.") 
       .setPositiveButton("Go ahead", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("AlertDialog", "Positive"); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("AlertDialog","Negative"); 
        } 
       }) 
       .show();  

      } 

    }); 

和例外是:

08-14 11:08:33.254: ERROR/AndroidRuntime(4726): FATAL EXCEPTION: main 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726): android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running? 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.view.ViewRoot.setView(ViewRoot.java:509) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.view.Window$LocalWindowManager.addView(Window.java:424) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.app.Dialog.show(Dialog.java:241) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.app.AlertDialog$Builder.show(AlertDialog.java:802) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:74) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.view.View.performClick(View.java:2408) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.view.View$PerformClick.run(View.java:8817) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.os.Handler.handleCallback(Handler.java:587) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.os.Looper.loop(Looper.java:144) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-14 11:08:33.254: ERROR/AndroidRuntime(4726):  at dalvik.system.NativeStart.main(Native Method) 

我試圖與thisMyCollecionId.this,與添加上下文廣告etc.but我不能得到的東西work.Any想法如何解決這個問題?

+1

這是東陽您的活動沒有運行。你爲什麼在構造函數中使用getParent()?嘗試使用MyActivity.this(AlertDialog所在的位置),然後檢查在顯示對話框 – manelizzard

+0

之前沒有「完成」您的活動。我只能在後退按鈕中的setOnClickListener中完成任何操作。 –

回答

1
Button deactivate = (Button) findViewById(R.id.deactivate); 
     deactivate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       new AlertDialog.Builder(yourActivity.this) 
       .setTitle("Warning") 
       .setMessage("The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection.") 
       .setPositiveButton("Go ahead", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("AlertDialog", "Positive"); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("AlertDialog","Negative"); 
        } 
       }) 
       .show();  

      } 

    }); 

嘗試一下,通過你的跑步活動的背景下

+0

我試過了,沒有工作 –

1

試試這個:

Button deactivate = (Button) findViewById(R.id.deactivate); 
     deactivate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
      if(!isFinishing()){ 
       new AlertDialog.Builder(yourActivity.this) 
       .setTitle("Warning") 
       .setMessage("The collection will be removed completely from the device.You can reactivate it later again.This operation requires internet connection.") 
       .setPositiveButton("Go ahead", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("AlertDialog", "Positive"); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d("AlertDialog","Negative"); 
        } 
       }) 
       .show();  
       } 
      } 

    }); 
+0

其實我用getParent()解決了我的問題,並在我的TabGroupActivity類中做了一些修改。我看到問題出在哪裏。但是,無論如何要謝謝。我知道這段代碼也在工作,我會接受答案。 –

相關問題