我试图使用意图将对象从服务类发送到MainActivity,但我总是收到该异常。 我的服务类别意图。
private List<Song> songsList;
@Override
public void onCreate(){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("songsList", (Serializable) songsList);
startActivity(intent);
}
我的活动收到意图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songsList = findViewById(R.id.songsList);
songsList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
startService(new Intent(this, MediaPlayerHolder.class));
baseSongList = (ArrayList<Song>)getIntent().getSerializableExtra("songsList");
songsListView.addAll(baseSongList);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, songsListView);
songsList.setAdapter(arrayAdapter);
}
仅当我尝试将多余内容投射到ArrayList时,才会发生异常。
07-09 17:05:44.079 2113-2113/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.simplemusicplayer, PID: 2113
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simplemusicplayer/com.simplemusicplayer.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2724)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2785)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6337)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
at java.util.ArrayList.addAll(ArrayList.java:562)
at com.simplemusicplayer.MainActivity.onCreate(MainActivity.java:86)
at android.app.Activity.performCreate(Activity.java:6858)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2677)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2785)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6337)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
答案 0 :(得分:1)
如果尚未从服务中初始化歌曲列表。
将您的歌曲设为可打包,然后使用放置列表
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putParcelableArrayListExtra("songsList", songsList);
startActivity(intent);
并使用
进行活动baseSongList = (ArrayList<Song>)getIntent().getParcelableArrayListExtra("songsList");