按下 Home 后应用程序崩溃。无法暂停 - 派生类未调用 super.onSaveInstanceState()

发布于 2024-11-02 04:41:52 字数 2566 浏览 3 评论 0原文

当我按下主页按钮时,应用程序应该暂停,保存所有状态并正常工作。 相反,我收到此错误:

java.lang.RuntimeException:无法 暂停活动 {be.test.tester/be.test.tester.DataScreen}: java.lang.IllegalStateException: 派生类没有调用 super.onSaveInstanceState() 位于 android.app.ActivityThread.performPauseActivity(ActivityThread.java:3641) 在 android.app.ActivityThread.performPauseActivity(ActivityThread.java:3598) 在 android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3574) 在 android.app.ActivityThread.access$2500(ActivityThread.java:136) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2186) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:143) 在 android.app.ActivityThread.main(ActivityThread.java:5068) 在 java.lang.reflect.Method.invokeNative(本机 方法)在 java.lang.reflect.Method.invoke(Method.java:521) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 在 dalvik.system.NativeStart.main(本机 方法)

原因: java.lang.IllegalStateException: 派生类没有调用 super.onSaveInstanceState() 位于 android.view.View.dispatchSaveInstanceState(View.java:6087) 在 android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1207) 在 android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1207) 在 android.view.View.saveHierarchyState(View.java:6068) 在 com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1475) 在 android.app.Activity.onSaveInstanceState(Activity.java:1106) 在 android.app.Activity.performSaveInstanceState(Activity.java:1056) 在 android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1289) 在 android.app.ActivityThread.performPauseActivity(ActivityThread.java:3623) ... 12 更多

我的活动对触摸做出反应:

public class DataScreen extends Activity implements OnGestureListener{   

我从意图中获得了一些额外的内容:

totUsage = Integer.parseInt(getIntent().getStringExtra("extraTotUsage"));
    limit = Integer.parseInt(getIntent().getStringExtra("extraLimit"));
    Bundle bundle = getIntent().getExtras();
    mylist = (ArrayList<HashMap<String, String>>) bundle.get("extraMyList");

自定义视图正在显示数据(画布)。当您在屏幕上滚动时,自定义视图(设置、获取方法)中的数据会发生变化并自行重绘。

我在这里并没有真正管理 onSaveInstanceState,我真的不知道是否必须这样做。

我的应用程序位于堆栈顶部,因为:

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

我不明白该错误。

When I press the Home button, the app should be paused, save all state and work fine.
Instead I get this error:

java.lang.RuntimeException: Unable to
pause activity
{be.test.tester/be.test.tester.DataScreen}:
java.lang.IllegalStateException:
Derived class did not call
super.onSaveInstanceState() at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3641)
at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3598)
at
android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3574)
at
android.app.ActivityThread.access$2500(ActivityThread.java:136)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2186)
at
android.os.Handler.dispatchMessage(Handler.java:99)
at
android.os.Looper.loop(Looper.java:143)
at
android.app.ActivityThread.main(ActivityThread.java:5068)
at
java.lang.reflect.Method.invokeNative(Native
Method) at
java.lang.reflect.Method.invoke(Method.java:521)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at
dalvik.system.NativeStart.main(Native
Method)

Caused by:
java.lang.IllegalStateException:
Derived class did not call
super.onSaveInstanceState() at
android.view.View.dispatchSaveInstanceState(View.java:6087)
at
android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1207)
at
android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:1207)
at
android.view.View.saveHierarchyState(View.java:6068)
at
com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1475)
at
android.app.Activity.onSaveInstanceState(Activity.java:1106)
at
android.app.Activity.performSaveInstanceState(Activity.java:1056)
at
android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1289)
at
android.app.ActivityThread.performPauseActivity(ActivityThread.java:3623)
... 12 more

My activity reacts on touch:

public class DataScreen extends Activity implements OnGestureListener{   

I'm getting some extra's from the intent:

totUsage = Integer.parseInt(getIntent().getStringExtra("extraTotUsage"));
    limit = Integer.parseInt(getIntent().getStringExtra("extraLimit"));
    Bundle bundle = getIntent().getExtras();
    mylist = (ArrayList<HashMap<String, String>>) bundle.get("extraMyList");

A custom view is showing data (canvas). When you scroll on screen, data changes in custom view (set, get method) and redraws itself.

I don't really manage the onSaveInstanceState here, don't really know if I have to.

My app is onTop of the stack, because of:

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

I don't understand the error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的温暖 2024-11-09 04:41:52

您应该重写 onSaveInstanceState 并调用其超级方法,如下所示。至少它帮助我解决了同样的问题。

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // your stuff or nothing
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // your stuff or nothing
}

You should override onSaveInstanceState and call its super method like shown below. At least it helped me with the same problem.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    // your stuff or nothing
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // your stuff or nothing
}
终弃我 2024-11-09 04:41:52

我刚刚正在处理这个问题。在我的应用程序中,主活动允许调用第二个活动来显示信息/说明等,它还有一个绘制应用程序的运行时元素的表面视图。

主要活动有一个变量来保存表面视图,如果在调用信息/指令活动之前尚未设置该变量,我会收到此错误。我只是将表面视图变量分配移至主活动 onCreate 中,一切都很好。

I've just been dealing with this problem. In my app the main activity allows a second activity to be called to display info/instructions etc, it also has a surface view that draws the runtime element of the app.

The main activity has a variable to hold the surface view, if that variable hasn't been set before the info/instructions activity is called I get this error. I simply moved the surface view variable assignment into the main activity onCreate and all is well.

蓝眼睛不忧郁 2024-11-09 04:41:52

也有可能使用不调用“super.onSavedInstanceState()”的自定义首选项(例如自定义对话框首选项),这会导致相同的错误。

It is also possible that one uses a custom preference (e.g. custom dialog preference) that does not call "super.onSavedInstanceState()" which leads to the same error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文