5

我有以下代码

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent != null) {
        Log.i("INTENT", intent.getAction().toString());
    }
    return START_STICKY;
}

但它总是NullPointerException在线返回:

Log.i("INTENT", intent.getAction().toString());

为什么?如果“intent”变量不为空,我正在检查上面。如果是这种情况,请执行以下代码。但我仍然得到空指针异常。

服务是从这样的活动开始的:

startService(new Intent(this, MainService.class));

我究竟做错了什么?

4

2 回答 2

14

你得到一个NullPointerException因为intent.getAction()似乎返回null

你应该把你的检查扩展到这个:

if(intent != null && intent.getAction() != null) {

如果你想向你的意图添加一个动作,你需要调用setAction()

Intent i = new Intent(this, MainService.class);
i.setAction("foo");
startService(i);
于 2013-04-16T17:22:04.137 回答
0

我得到了类似的堆栈,添加检查

intent.getAction() != null

有效。

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.languoguang.welcomeapp, PID: 17676
              java.lang.RuntimeException: Unable to start service com.example.languoguang.welcomeapp.CountService@f6049d4 with Intent { cmp=com.example.languoguang.welcomeapp/.CountService }: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3698)
                  at android.app.ActivityThread.access$1500(ActivityThread.java:198)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6649)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                  at com.example.languoguang.welcomeapp.CountService.onStartCommand(CountService.java:24)
                  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3679)
                  at android.app.ActivityThread.access$1500(ActivityThread.java:198) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1668) 
                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6649) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826) 

I/Process:发送信号。PID:17676 SIG:9

于 2018-05-31T02:10:03.130 回答