【问题标题】:Android Childactivity getParent question?Android Childactivity getParent问题?
【发布时间】:2011-08-15 12:50:15
【问题描述】:

基本上我在我的应用程序中使用 ActivityGroup。我有这种情况:

我有带有活动 A 的 Tabhost。

Activity A 创建 childActivity B。

A  --->  B

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

活动 B 创建 3 个子活动 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));

B 也创建了另一个 childActivity,将其命名为 E。

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

所以基本上活动 C 和 D 也可以启动活动 E,使用:

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

我已经重写了 onBackPressed 方法,所以我可以控制后退按钮。它看起来像这样:

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

            }

    });

当我从 A 启动活动 E 时,当我单击按钮时,将显示警报对话框,一切正常。但是当我从 C 或 D 启动活动 E 时,它会抛出异常:

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 android.app.LocalActivityManager$LocalActivityRecord@4630ea20 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)

【问题讨论】:

    标签: android android-alertdialog activitygroup


    【解决方案1】:

    你可以做一件事 在ActivityA.java中

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

    【讨论】:

    • 错误:ActivityB.parentActivity 无法解析为类型。
    • 您没有完成正确的分配检查您的 B 活动类名称,并确保您已将活动视为静态即(parentActitvity)
    • 我在 E 类中使用了公共静态活动 parentActivity(在 onCreate() 之前)。在 onCreate 我把 parentActivity=this;在 alertdialog 的代码中我做了: new AlertDialog.Builder(Recommended.parentActivity.this) (ActivityB=Recommended) 它显示了一个错误。
    • 在ActivityB中,声明parentActivity不在E中,在E中不需要声明任何东西,只需要AlertDialog.Builder(ActivityB.parentActivity.this)
    • 仍然抛出相同的异常,并在尝试添加Recommended.parentActivity.this时显示错误
    猜你喜欢
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多