如何在初始化意图时解决NullPointerException?

时间:2016-09-05 18:46:30

标签: java android android-intent nullpointerexception

我试图在下面这样的函数中初始化一个意图:

public void playNextOver() {
    songInfo detail = (songInfo) songAdapter.getItem(globalPos);

    songTitle = detail.getTitle();

    songPath = detail.getPath();
    songId = String.valueOf(detail.getID());
    songArtist = detail.getArtist();

    Intent globalIntentTemp = new Intent(getApplicationContext(), songPlay.class);
    globalIntentTemp.putExtra("SONG_ID", songId);
    globalIntentTemp.putExtra("SONG_TITLE", songTitle);
    globalIntentTemp.putExtra("SONG_ARTIST", songArtist);
    globalIntentTemp.putExtra("SONG_PATH", songPath);
    getApplicationContext().startActivity(globalIntentTemp);
}

我在活动中这样做。

但它在运行时在此声明中给出了以下错误

Intent globalIntentTemp = new Intent(getApplicationContext(), songPlay.class); 

09-06 00:32:19.441 25790-25790 / com.example.coderahul.beats E / AndroidRuntime:FATAL EXCEPTION:main                                                                              过程:com.example.coderahul.beats,PID:25790                                                                              java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.content.Context android.content.Context.getApplicationContext()'                                                                                  在android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)                                                                                  在com.example.coderahul.beats.playlistManager.playNextOver(playlistManager.java:503)                                                                                  在com.example.coderahul.beats.songPlay $ 2.onClick(songPlay.java:703)                                                                                  在android.view.View.performClick(View.java:4848)                                                                                  在android.view.View $ PerformClick.run(View.java:20299)                                                                                  在android.os.Handler.handleCallback(Handler.java:815)                                                                                  在android.os.Handler.dispatchMessage(Handler.java:104)                                                                                  在android.os.Looper.loop(Looper.java:218)                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5657)                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                  在java.lang.reflect.Method.invoke(Method.java:372)                                                                                  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:990)                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:785)

我不知道该做什么,因为我是Android新手。请帮忙。

0 个答案:

没有答案