这是一个解决不同问题的问题。
我将两个字符串从我的主活动传递给子服务,这很好,但是当主活动消失时,服务会抛出一个NullPointerException来尝试抓取两个字符串。
来自MainActivity:
Intent i = new Intent(this, PebbleService.class);
i.putExtra("quote", quote[0]);
i.putExtra("author", quote[1]);
startService(i);
来自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;
}
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)
答案 0 :(得分:2)
intent - ...如果重新启动服务,则可能为null
因此请检查null
意图:
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");
...
}