2011-08-15 73 views
1

基本上我在我的應用程序中使用ActivityGroup。我有這種情況:Android的Childactivity getParent問題?

我有Tabhost與活性A.

活性A創建childActivity B.

A ---> B 

startChildActivity("CollectionList", new Intent(this,MyCollectionList.class)); 

活性B創建3個childactivities C,D.

B ---> C (childActivity of B) 
startChildActivity("Recommended", new Intent(MyCollectionList.this,Recommended.class)); 


B ---> D (childActivity of B) 
startChildActivity("ExpectSoon", new Intent(MyCollectionList.this,ExpectSoon.class)); 

乙創建另一個childActivity,命名爲E.

B ---> E 
Intent previewMessage = new Intent(getParent(), MyCollectionId.class); 
TabGroupActivity parentActivity = (TabGroupActivity)getParent(); 
parentActivity.startChildActivity("MyCollectionId", previewMessage); 

所以基本上活動C和d可以開始動鄂也與:

  Intent previewMessage = new Intent(getParent(), MyCollectionId.class); 
      TabGroupActivity parentActivity = (TabGroupActivity)getParent(); 
      parentActivity.startChildActivity("MyCollectionId", previewMessage); 

我不得不重寫onBackPressed方法,所以我可以控制回button.It看起來是這樣的:

private ArrayList<String> mIdList; 

@Override 
    public void onBackPressed () { 
     int length = mIdList.size(); 
     if (length >=1) { 
      Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1)); 
      current.finish(); 
     } 
    } 

所以我的問題是,當我在活動E,並按下後退按鈕我的應用程序關閉。而我的另一個問題是與活動E中的警報對話框。

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();  

      } 

    }); 

當我從一開始活動E,當我按一下按鈕,它會顯示警告對話框中,一切都是ok.But當我從C或d開始動鄂它拋出我的異常:

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

編輯:

我startChildActivity看起來是這樣的:

public void startChildActivity(String Id, Intent intent) {  
     Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); 
     if (window != null) { 
      mIdList.add(Id); 
      setContentView(window.getDecorView()); 
     } 

}

最新logcat的,而在ActivityE使用Recommended.parentActivity:

 new AlertDialog.Builder(Recommended.parentActivity) 


08-15 16:33:53.509: ERROR/AndroidRuntime(1967): FATAL EXCEPTION: main 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967): java.lang.NullPointerException 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at com.android.internal.app.AlertController$AlertParams.<init>(AlertController.java:743) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at android.app.AlertDialog$Builder.<init>(AlertDialog.java:273) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at com.stampii.stampii.mystampii.MyCollectionId$4.onClick(MyCollectionId.java:62) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at android.view.View.performClick(View.java:2408) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at android.view.View$PerformClick.run(View.java:8817) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at android.os.Handler.handleCallback(Handler.java:587) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at android.os.Handler.dispatchMessage(Handler.java:92) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at android.os.Looper.loop(Looper.java:144) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at android.app.ActivityThread.main(ActivityThread.java:4937) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at java.lang.reflect.Method.invoke(Method.java:521) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
08-15 16:33:53.509: ERROR/AndroidRuntime(1967):  at dalvik.system.NativeStart.main(Native Method) 

回答

1

你可以做ActivityA.java onething

public static Activity parentActivity; 

onCreate() 
{ 
    parentActivity=this; 
} 
// start your child activity ie(E) 

ActivityE.java

Button deactivate = (Button) findViewById(R.id.deactivate); 
     deactivate.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       new AlertDialog.Builder(ActivityA.parentActivity.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

錯誤:ActivityB.parentActivity不能被解析爲類型。 –

+0

你沒有做適當的任務檢查你的B活動類名稱,並確保你已經採取了靜態活動即(parentActitvity) –

+0

我在類E(在onCreate()之前)使用公共靜態活動parentActivity。在onCreate我把parentActivity = this;並在alertdialog的代碼,我做了:新AlertDialog.Builder(Recommended.parentActivity.this)(ActivityB =推薦),它顯示我一個錯誤。 –