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

标签 java android service android-emulator

我在两者上都使用 Marshmallow,所以我不明白为什么我的应用程序在模拟器上稳定,但在手机上却不稳定。它可以工作,但很快就会崩溃。这是日志猫:

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

最佳答案

查看 Service 的文档。以下是当您从 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. 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; 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.

这正是您的情况所发生的情况,您的 intent 参数得到一个 null 。您应该检查 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).

关于java - 应用程序在模拟器上运行良好,但在手机上经常崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41553311/

相关文章:

java - 下载文件时文件名问题中的空格

android - 您如何强制嵌入式音频文件在Android Tablet上自动启动?

android - 使用 Google +/PlusClient 获取个人信息

sql - SSIS部署: The task <task name> cannot run on this edition of integration services.需要更高级别的版本

android - Service 的 stopSelfResult 方法的目的是什么?真正的用例将是完美的。

java - HttpsURLConnection 使用了哪种安全技术?

c# - java 和 xna(c#) 中继承语法的区别

Java从带有撇号和连字符的文件中读取单词并将它们计为2个单词

android - 在应用程序上为不同的 Android 版本创建 - 在较低版本上可用,仅在较高版本中导入

c# - 如何在启用 COM 互操作的 DLL 中引用 WCF 服务?