简体   繁体   English

Android:一段时间后重新打开应用程序时出现无法解释的崩溃问题

[英]Android: Unexplained crashes when the app is reopened after a while

I have a bunch of activities A->B->C->D and they all work well. 我有一堆活动A-> B-> C-> D,它们都运作良好。 I have tested them with and without internet, multiple configurations and I have triggered the screen to rotate in every of them to see if they would crash. 我已经测试过它们有无互联网,多种配置,我已触发屏幕旋转每一个,看看它们是否会崩溃。 They didn't 他们没有

But on my Android phone (not emulator), after a while that the app is in the background, if I open it back (either by recents or by my main menu), I get a series of unexplicable crashes. 但是在我的Android手机(不是模拟器)上,经过一段时间该应用程序在后台,如果我打开它(通过最近或我的主菜单),我得到一系列无法​​解释的崩溃。 It's literally activity D crashes, goes to C. C crashes, goes to B. B crashes, goes to A. A crashes, resets, and opens fine. 它实际上是活动D崩溃,转向C. C崩溃,转向B. B崩溃,转到A.崩溃,重置,并打开罚款。 It drives me nuts. 它让我疯了。

It's always NullPointerException on objects, like these ones: 它总是在对象上出现NullPointerException ,如下所示:

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.getfoodie.foodie/net.getfoodie.foodie.ui.exploremenu.MenuFragmentActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at net.getfoodie.foodie.ui.exploremenu.MenuGridAdapter.<init>(MenuGridAdapter.java:71)
at net.getfoodie.foodie.ui.exploremenu.MenuFragment.onActivityCreated(MenuFragment.java:75)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1794)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:544)
at net.getfoodie.foodie.ui.exploremenu.MenuFragmentActivity.onStart(MenuFragmentActivity.java:89)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1189)
at android.app.Activity.performStart(Activity.java:5436)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
... 12 more 

And for example, the error above is because of a null object from the parse sdk. 例如,上面的错误是因为解析sdk中的null对象。 But the object has already been fetched during the OnCreate of the activity, and I guess it gets lost when the activity is recreated? 但是在活动的OnCreate期间已经获取了该对象,我猜它在重新创建活动时会丢失? although I make sure that I fetch it in my OnCreate() , and I tested the rotation? 虽然我确保在OnCreate()获取它,然后测试了旋转?

What do you think this is due to? 您认为这是由于什么原因造成的? Am I missing something in my app lifecycle? 我在应用生命周期中遗漏了什么吗?

And more importantly, how can I trigger the "activity kill/sleep" on my Android emulator so I can reproduce the issue and actually try to fix it? 更重要的是,如何在我的Android模拟器上触发“活动杀死/睡眠”,以便重现问题并实际尝试修复它?

Activities not on screen are frequently killed by Android for memory. 不在屏幕上的活动经常被Android内存杀死。 When coming back to an Activity via recents, the most recent activity will be launched but if it was killed it will have no context- it will get the intent it was launched with but have no static variables or singletons assigned to previous values. 当通过最近回到活动时,最近的活动将被启动但是如果它被杀死它将没有上下文 - 它将获得它启动的意图但没有分配给先前值的静态变量或单例。 This causes problems for a lot of apps. 这会导致许多应用程序出现问题。 The solution is to either allow any activity to fill out these values somehow, or to detect this case and launch an appropriate activity with CLEAR_TOP set to kill the old state. 解决方案是允许任何活动以某种方式填写这些值,或者检测此情况并启动适当的活动,并将CLEAR_TOP设置为终止旧状态。

Oh, and to reproduce it all you need is force stop from the activites manager settings. 哦,重现它你需要的是从活动管理器设置强制停止。 Go to activity D, then force stop. 转到活动D,然后强制停止。 Then return via recents. 然后通过最近返回。

As an example- I have an app with a LoginActivity, and a MainActivity. 作为一个例子 - 我有一个带有LoginActivity和MainActivity的应用程序。 MainActivity assumes LoginActivity has set up a user singleton. MainActivity假设LoginActivity已设置用户单例。 My code in MainActivity is: 我在MainActivity中的代码是:

public void onCreate(Bundle bundle){
  super.onCreate(bundle);
  if(User.getCurrentUser() == null){
    Intent intent = new Intent(this, LoginActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
    return;
  }
  //Continue on with onCreate for this activity
}

This will relaunch the login activity if the User object wasn't set up. 如果未设置User对象,这将重新启动登录活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM