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

时间:2017-01-09 17:12:51

标签: java android service android-emulator

我正在使用Marshmallow,所以我不明白为什么我的应用程序在模拟器上稳定但在手机上不稳定。它很有效但很快崩溃了。这是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) 

和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;
}

第45行是以" item"开头的那一行。 一些指示我真的很感激。如果需要,我可以发布更多代码。谢谢!

编辑:Logcat使用START__REDELIVER_INTENT

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

2 个答案:

答案 0 :(得分:1)

查看Service的文档。以下是从START_STICKY返回onStartCommand()时发生的情况:

  

如果此服务的进程在启动时被终止(从onStartCommand(Intent,int,int)返回后),则将其保持在启动状态但不保留此传递的意图。稍后系统将尝试重新创建服务。因为它处于启动状态,所以它将保证在创建新服务实例后调用onStartCommand(Intent,int,int);如果没有任何挂起的启动命令要传递给服务,它将使用null intent对象调用,因此您必须注意检查这一点。

这正是您的情况,您获得null参数的intent。您应该检查null,或者返回START_REDELIVER_INTENT,其行为如下:

  

如果此服务的进程在启动时被终止(从onStartCommand(Intent,int,int)返回后),那么它将被安排重新启动,最后一次传递的Intent再次传递给它通过onStartCommand(Intent,int,int)。

答案 1 :(得分:0)

我建议您首先检查您的意图是否为空,然后执行相关操作,如

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

系统重新启动某个时间STICKY_SERVICE,只要它自己被破坏,因此意图可能为空