2017-01-09 131 views
0

我对两者都使用棉花糖,所以我不明白为什么我的应用程序在模拟器上稳定,但不在手机上。它可以工作,但很快就会崩溃。这在logcat的:应用程序在模拟器上运行良好,但经常在手机上崩溃

Process: com.madhatter.nat.test, PID: 28210 
java.lang.RuntimeException: Unable to start service [email protected] 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)  

和onStartCommand在OverlayService:

@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行是与 “项目” 启动一个。 我会很感激一些指针。如果需要,我可以发布更多代码。谢谢!

编辑: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 
+0

可能重复[什么是NullPointerException,以及如何解决它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i - 修复它) –

回答

1

检查文档Service。这里是发生了什么,当您返回START_STICKYonStartCommand()

如果在它启动(从onStartCommand(意向,INT,INT)返回后)这个服务的过程中被杀死了,然后把它留在启动状态,但不要”不保留这个交付的意图。稍后系统将尝试重新创建该服务。因为它处于启动状态,它将保证在创建新的服务实例后调用onStartCommand(Intent,int,int);如果没有任何挂起的启动命令要传递给服务,它将被调用一个空意图对象,所以你必须小心检查这一点。

这正是你的情况,你会得到一个nullintent参数。您应该检查null,或返回START_REDELIVER_INTENT,它具有以下行为:

如果在它启动(从onStartCommand(意向,INT,INT)返回后)这个服务的过程中被杀死了,然后它会按计划重新启动,最后一次传递的Intent通过onStartCommand(Intent,int,int)重新传递给它。

+0

非常感谢。检查null不会有帮助。但是,返回START_REDELIVER_INTENT会有一个警告:有一个初始崩溃。然后,服务在一段时间后再次启动,并且不会再次崩溃。有任何想法吗? :) –

+0

看来,最初没有最后传递的意图,我们需要在onStartCommand的首次调用时成功返回START_REDELIVER_INTENT。所以第一次崩溃。 – ashubuntu

+0

服务在我的设备上而不是在我的模拟器上崩溃的原因是该服务正在被杀死以使内存可用于其他应用程序。解决方案是以前台服务的形式开始服务。我使用的文档是https://android-developers.googleblog.com/2010/02/service-api-changes-starting-with.html非常感谢您的帮助人员,因为它指向了正确的方向。 –

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每当它本身被破坏,因此可能意图为空

+0

非常感谢,但这似乎没有任何区别... –

相关问题