简体   繁体   English

应用在模拟器上运行良好,但经常在手机上崩溃

[英]App runs well on emulator but frequently crashes on phone

I am using Marshmallow on both so I don't understand why my app is stable on the emulator but not on my phone. 我在两者上都使用棉花糖,所以我不明白为什么我的应用程序在模拟器上稳定,但在手机上却不稳定。 It works but crashes fairly soon after. 它可以工作,但很快就会崩溃。 This the logcat: 这个logcat:

Process: com.madhatter.nat.test, PID: 28210
java.lang.RuntimeException: Unable to start service com.madhatter.nat.test.OverlayService@89014f1 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap17(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
at com.madhatter.nat.test.OverlayService.onStartCommand(OverlayService.java:45)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3028)
at android.app.ActivityThread.-wrap17(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5443) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

and the onStartCommand in OverlayService: 以及OverlayService中的onStartCommand:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    item = intent.getExtras().getParcelable(DataItemAdapter.ITEM_KEY);
    if (item == null) {
        throw new AssertionError("Null data item received!");
    } else {
        getImageDrawable();
    }
    return START_STICKY;
}

Line 45 is the one which starts with "item". 第45行是从“ item”开始的行。 I would be really grateful for some pointers. 我真的很感谢一些提示。 I can post more code if needed.Thanks! 如果需要,我可以发布更多代码。谢谢!

EDIT: Logcat using START__REDELIVER_INTENT 编辑:使用START__REDELIVER_INTENT的Logcat

01-10 09:18:09.144 1840-7536/? I/WindowState: WIN DEATH: Window{6594f29 u0 com.madhatter.nat.test/com.madhatter.nat.test.MainActivity}
01-10 09:18:09.147 1840-7538/? I/WindowState: WIN DEATH: Window{cee56ff u0 com.madhatter.nat.test}
01-10 09:18:09.151 1840-6620/? I/WindowState: WIN DEATH: Window{26fc259 u0 com.madhatter.nat.test}
01-10 09:18:09.154 1840-7539/? I/ActivityManager: Process com.madhatter.nat.test (pid 29128) has died
01-10 09:18:09.154 1840-7539/? W/ActivityManager: Scheduling restart of crashed service com.madhatter.nat.test/.OverlayService in 32668ms
01-10 09:18:09.157 1840-3383/? W/InputDispatcher: channel '7370c7b com.madhatter.nat.test/com.madhatter.nat.test.SelectionPage (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
01-10 09:18:09.157 1840-3383/? E/InputDispatcher: channel '7370c7b com.madhatter.nat.test/com.madhatter.nat.test.SelectionPage (server)' ~ Channel is unrecoverably broken and will be disposed!
01-10 09:18:09.157 1840-3383/? W/InputDispatcher: channel 'dc69e50 com.madhatter.nat.test/com.madhatter.nat.test.LauncherPage (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
01-10 09:18:09.157 1840-3383/? E/InputDispatcher: channel 'dc69e50 com.madhatter.nat.test/com.madhatter.nat.test.LauncherPage (server)' ~ Channel is unrecoverably broken and will be disposed!
01-10 09:18:09.159 1840-4579/? I/WindowState: WIN DEATH: Window{dc69e50 u0 com.madhatter.nat.test/com.madhatter.nat.test.LauncherPage}
01-10 09:18:09.159 1840-4579/? W/InputDispatcher: Attempted to unregister already unregistered input channel 'dc69e50 com.madhatter.nat.test/com.madhatter.nat.test.LauncherPage (server)'
01-10 09:18:09.162 1840-1851/? I/WindowState: WIN DEATH: Window{7370c7b u0 com.madhatter.nat.test/com.madhatter.nat.test.SelectionPage}
01-10 09:18:09.162 1840-1851/? W/InputDispatcher: Attempted to unregister already unregistered input channel '7370c7b com.madhatter.nat.test/com.madhatter.nat.test.SelectionPage (server)'
01-10 09:18:41.843 1840-1854/? I/ActivityManager: Start proc 32159:com.madhatter.nat.test/u0a125 for service com.madhatter.nat.test/.OverlayService
01-10 09:18:41.906 32159-32159/? W/System: ClassLoader referenced unknown path: /data/app/com.madhatter.nat.test-2/lib/arm

Check the documentation for Service . 检查Service的文档。 Here's what happens when you return START_STICKY from onStartCommand() : 当您从onStartCommand()返回START_STICKY时,将发生以下情况:

if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. 如果此服务的进程在启动时被杀死(从onStartCommand(Intent,int,int)返回之后),则将其保持在启动状态,但不要保留此传递的意图。 Later the system will try to re-create the service. 稍后,系统将尝试重新创建服务。 Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance; 因为它处于启动状态,所以它将保证在创建新服务实例后调用onStartCommand(Intent,int,int); if there are not any pending start commands to be delivered to the service, it will be called with a null intent object, so you must take care to check for this. 如果没有任何待处理的启动命令要传递给服务,则将使用空意图对象调用该命令,因此您必须注意进行检查。

This is exactly what happens in your case, you get a null for the intent parameter. 这正是您遇到的情况, intent参数为null You should either check for null , or return START_REDELIVER_INTENT , which has the following behavior: 您应该检查是否为null ,或者返回START_REDELIVER_INTENT ,它具有以下行为:

if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last delivered Intent re-delivered to it again via onStartCommand(Intent, int, int). 如果此服务的进程在启动时被杀死(从onStartCommand(Intent,int,int)返回之后),则将计划重新启动该进程,并通过onStartCommand(Intent,int, int)。

I suggest you to first check if your intent is null or not and then perform the related operation like, 我建议您先检查您的意图是否为空,然后执行相关操作,例如,

if(intent != null){
    item = intent.getExtras().getParcelable(DataItemAdapter.ITEM_KEY);
    if (item == null) {
       throw new AssertionError("Null data item received!");
    } else {
       getImageDrawable();
    } 
}

Some time STICKY_SERVICE restarted by system whenever it gets destroyed itself so it may possible that intent is null STICKY_SERVICE会在销毁自身时被系统重新启动,因此intent可能为null

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

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