我正面臨一個小問題。嘗試從非空數據包獲取數據時的Nullpointerexception
我試圖將播放列表對象傳遞給我的媒體播放器服務。
在其onStartCommand
中,我在嘗試撥打extras.get()
或extras.getParcelable()
時收到Nullpointer Exception
。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
L.d(this, "Service started");
Bundle extras = intent.getExtras();//Not null
if (extras == null) {
stopSelf();
return 0;
}
L.d(this, "Extras was " + (extras == null ? "null" : "not null"));
defaultPlaylist = extras.getParcelable("playlist");//Nullpointer
currentSongNumber = extras.getInt(INITIAL_SONG_NUMBER);//Nullpointer
registerReceiver(getApplicationContext());
new NotificationPlayerControl(getApplicationContext());
NotificationPlayerControl.update(defaultPlaylist.getSong(currentSongNumber));
return START_STICKY;
}
extras
不爲空。
這是怎麼回事?
編輯: 看來,當我試圖將我的parcelable對象寫入Bundle時發生錯誤。我排除它,現在運行良好。
也許堆棧跟蹤只是有點誤導
您是否嘗試過直接調用'intent.getParcelableExtra(「播放列表」)'? – ujvl
是的,仍然是同樣的問題。我也試過getInt,它既不工作也不工作 –
好的,可能是'playlist'是拼寫不正確的鍵。檢查您傳遞對象時的密鑰和接收服務時的密鑰是否相同。如果不存在關鍵字類型的映射,則它將返回null。 – ujvl