尝试从非空的bundle获取数据时的Nullpointerexception

时间:2014-03-05 15:22:35

标签: android android-intent nullpointerexception parcelable

我正面临一个小问题。

我正在尝试将播放列表对象传递给我的媒体播放器服务。

在其onStartCommand中,我在尝试拨打Nullpointer Exceptionextras.get()时收到extras.getParcelable()

@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时,似乎发生了错误。我排除了它,现在它运行良好。

也许Stacktrace只是有点误导

1 个答案:

答案 0 :(得分:0)

您正在检查,但在检查后仍然可以访问它:

if (extras == null || defaultPlaylist == null) {
        stopSelf();
        return;
 } 
    L.d(this,"Extras was "+(extras==null?"null":"not null"));
   extras.getParcelable("playlist");