Passing extras from Activity to Intent throws Null

2019-08-14 03:47发布

问题:

This is a problem that forked off a different issue.

I'm passing two strings from my main activity to a child service, which is fine, but when the main activity dies, the service throws a NullPointerException trying to grab the two strings.

From MainActivity:

Intent i = new Intent(this, PebbleService.class);
i.putExtra("quote", quote[0]);
i.putExtra("author", quote[1]);
startService(i);

From PebbleService:

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    final String author = intent.getStringExtra("author");
    final String quote = intent.getStringExtra("quote");

    // Define AppMessage behavior
    if (appMessageReciever == null) {
        appMessageReciever = new PebbleKit.PebbleDataReceiver(WATCHAPP_UUID) {

            @Override
            public void receiveData(Context context, int transactionId, PebbleDictionary data) {
                // Always ACK
                PebbleKit.sendAckToPebble(context, transactionId);

                // Send KEY_QUOTE to Pebble
                PebbleDictionary out = new PebbleDictionary();
                out.addString(KEY_QUOTE, quote);
                out.addString(KEY_AUTHOR, author);
                PebbleKit.sendDataToPebble(getApplicationContext(), WATCHAPP_UUID, out);
            }
        };

        // Add AppMessage capabilities
        PebbleKit.registerReceivedDataHandler(this, appMessageReciever);
    }

    return START_STICKY;
}

Error from Logcat:

05-26 10:29:52.972    4147-4147/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: net.thevgc.quotes, PID: 4147
    java.lang.RuntimeException: Unable to start service net.thevgc.quotes.PebbleService@423eb138 with null: java.lang.NullPointerException
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2732)
            at android.app.ActivityThread.access$2100(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at net.thevgc.quotes.PebbleService.onStartCommand(PebbleService.java:34)
            at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2715)
            at android.app.ActivityThread.access$2100(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1307)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

回答1:

As per the documentation:

intent - ... This may be null if the service is being restarted

Therefore check for null intent:

public int onStartCommand(Intent intent, int flags, int startId) {
    int cmd = super.onStartCommand(intent, flags, startId);

    if (intent == null) return cmd;

    final String author = intent.getStringExtra("author");
    final String quote = intent.getStringExtra("quote");

    ...
}