2016-12-31 150 views
0

我想發送一個parcelable對象到活動和服務。對於發送這兩個我用:Android可parcelable對象傳遞到活動,但不傳遞到服務

public void onClick(View view) { 
    Intent intent = new Intent(mContext, MainActivity.class); 
    Intent i = new Intent(mContext, OverlayService.class); 
    intent.putExtra(ITEM_KEY, item); 
    i.putExtra(ITEM_KEY, item); 
    mContext.startActivity(intent); 
} 

然後接受,在我的活動:

DataItem item = getIntent().getExtras().getParcelable(DataItemAdapter.ITEM_KEY); 
    if (item == null) { 
     throw new AssertionError("Null data item received!"); 
    } 

這完美的作品。但是,對於我的服務,我使用:

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    DataItem item = intent.getExtras().getParcelable(DataItemAdapter.ITEM_KEY); 
    if (item == null) { 
     throw new AssertionError("Null data item received!"); 
    }else{ 
     Toast.makeText(this, "Item received", Toast.LENGTH_SHORT).show(); 
    } 
    return START_STICKY; 
} 

結果是一個空指針異常:

Unable to start service [email protected] with Intent { cmp=com.madhatter.nat.test/.OverlayService }: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference 
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3314) 
at android.app.ActivityThread.-wrap21(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference 
at com.madhatter.nat.test.OverlayService.onStartCommand(OverlayService.java:46) 
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3297) 
at android.app.ActivityThread.-wrap21(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:154)  
at android.app.ActivityThread.main(ActivityThread.java:6077)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

這裏是我用來啓動服務的代碼:

public void show(View view) { 
    checkPermissionOverlay(); 
    Intent svc = new Intent(this, OverlayService.class); 
    startService(svc); 
    notificationService(); 
} 

任何想法我做錯了嗎?謝謝你的幫助。

+0

顯示您用來啓動服務的代碼。 – riggaroo

+0

我已添加到要求的代碼中。 –

回答

1

您需要在您用來啓動服務的意圖中包含額外項目,這與您開始活動的方式相同。

public void show(View view) { 
    checkPermissionOverlay(); 
    Intent svc = new Intent(this, OverlayService.class); 
    svc.putExtra(DataItemAdapter.ITEM_KEY, dataItem); 
    startService(svc); 
    notificationService(); 
} 
相關問題