我正面临一个小问题。尝试从非空数据包获取数据时的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