在使用服务机器人关闭时应用程序崩溃,因为即时尝试使用意图

时间:2016-03-21 20:13:16

标签: android android-intent service

public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getExtras() != null) {
        String temp = intent.getStringExtra("key");
        temp1 = Integer.parseInt(temp);
        counter = temp1;
    }
}

我正在将数据传递给服务,当应用程序打开时它会完美运行但是一旦我关闭应用程序就会崩溃。我已经尝试了很多东西而且我是android的新手。任何帮助将不胜感激

提前致谢。

这是日志

03-21 15:35:07.868 21818-21818 /? E / AndroidRuntime:致命异常:主要                                                    处理:com.cs442.shash5259.assignment_6,PID:21818                                                    java.lang.RuntimeException:无法使用null启动服务com.cs442.shash5259.assignment_6.Myservice@9d88461:java.lang.NullPointerException:尝试调用虚拟方法'android.os.Bundle android.content.Intent.getExtras() '在空对象引用上                                                        在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3045)                                                        在android.app.ActivityThread.-wrap17(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1452)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:148)                                                        在android.app.ActivityThread.main(ActivityThread.java:5443)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)                                                     引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.os.Bundle android.content.Intent.getExtras()'                                                        在com.cs442.shash5259.assignment_6.Myservice.onStartCommand(Myservice.java:55)                                                        在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3028)                                                        在android.app.ActivityThread.-wrap17(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1452)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:148)                                                        在android.app.ActivityThread.main(ActivityThread.java:5443)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:728)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

1 个答案:

答案 0 :(得分:5)

查看日志时,很明显,当应用关闭时,onStartCommand中的Intent对象为null

查看服务源代码中的documentation,提到:

  

如果服务,提供给Intent的{​​{1}}可能为null   正在重新启动之后        它的过程已经消失,它之前已经返回了任何东西         除了onStartCommand

根据您的应用的要求,您可以至少以两种不同的方式处理此问题。

1.您可以在onStartCommand中修改以下代码:

START_STICKY_COMPATIBILITY

这可以防止崩溃,但不会更新public int onStartCommand(Intent intent, int flags, int startId) { if (intent!= null) { String temp = intent.getStringExtra("key"); temp1 = Integer.parseInt(temp); counter = temp1; } }

2.或者您可以在服务中的counter回调中返回START_REDELIVER_INTENT,以便在重新启动后发送整个意图,从而阻止onStartCommand()成为Intent