gpt4 book ai didi

android: 当我点击后退按钮时应用程序崩溃 (activitygroup + TabWidget)

转载 作者:太空宇宙 更新时间:2023-11-03 10:27:40 25 4
gpt4 key购买 nike

每一个!

一年前,我开发了一个组合“ActivityGroup + TabWidget”的应用程序,它在 android os 2.3+ 上运行良好。但是我在 os 4.0 上测试过,我发现它有时会崩溃。- 如果我打开应用程序,我什么都不做,然后点击“返回”,效果很好。- 如果我打开应用程序,在应用程序中导航(单击列表项,查看详细信息项然后返回),然后当我返回到选项卡的“根”页面时,我单击“返回”,它崩溃了。 (它应该正常关闭应用程序)--------在这种情况下, Activity 组启动新 Activity 并收到返回键事件- 如果我打开应用程序,像上面那样在应用程序中导航,但只看到 View 不是由 Activity 组启动的,而是由组中的根 Activity 启动的。我工作得很好。

我的ActivityGroup中有一些代码

public void startActivity(Intent intent) {
// Start the root activity withing the group and get its view
View view = getLocalActivityManager().startActivity(id, intent)
.getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}

public void back() {
if(history.size() > 1) { //history = new ArrayList<View>(); global variable
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));

mIds.pop();
}else {
finish(); // I tried to commment it too, but no lucks
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
back();

return true;
}

return super.onKeyDown(keyCode, event);
}

还有错误堆栈:

05-18 18:47:47.687: E/AndroidRuntime(6828): FATAL EXCEPTION: main
05-18 18:47:47.687: E/AndroidRuntime(6828): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1213)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:442)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.Activity.onBackPressed(Activity.java:2121)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.Activity.onKeyUp(Activity.java:2099)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.KeyEvent.dispatch(KeyEvent.java:2557)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.Activity.dispatchKeyEvent(Activity.java:2329)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1859)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.widget.TabHost.dispatchKeyEvent(TabHost.java:298)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1859)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3300)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3273)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2436)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.os.Looper.loop(Looper.java:137)
05-18 18:47:47.687: E/AndroidRuntime(6828): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-18 18:47:47.687: E/AndroidRuntime(6828): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 18:47:47.687: E/AndroidRuntime(6828): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-18 18:47:47.687: E/AndroidRuntime(6828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-18 18:47:47.687: E/AndroidRuntime(6828): at dalvik.system.NativeStart.main(Native Method)

我没有使用Fragment,因为当我开发这个应用程序时它不是由android sdk发布的。

有什么想法吗?每个想法都会受到赞赏!!
谢谢!

最佳答案

您可以使用onBackPressed()来达到您想要的效果,您可以通过以下方式使用它。

public void onBackPressed(){
//Your code here
super.onBackPressed();
}

最重要的是确保您已在 Activity 组中使用的所有子 Activity 中实现此方法。

关于android: 当我点击后退按钮时应用程序崩溃 (activitygroup + TabWidget),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10658667/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com