0
我有一段創建歌曲ArrayList的片段,我想將該ArrayList傳遞給幀的活動。我知道我可以使用的界面,但不知道我怎麼能做到這一點將片段傳遞給自己的活動
public class SongsListFragment extends Fragment
{
public interface passArrayList {
public void onArticleSelected(Uri articleUri);
// from android guide i don't know what to do
}
}
這裏是我到目前爲止,請讓我知道爲什麼即時得到空指針
在MainActivity
@Override
public void onFragmentSetSongs(ArrayList<Song> songs){
songsArrayList = songs;
}
@Override
public void onSongListItemClick(int position) {
musicService.setSong(position);
Song playSong = songsArrayList.get(position);
txtCurrentSongTitle.setText(playSong.getTitle());
txtCurrentSongTitle.requestFocus();
musicService.playSong();
btnPlayPause.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_pause));
if (playbackPaused) {
playbackPaused = false;
}
//setDownloaded();
}
上哪裏我完成建設songArrayList的方法段
OnFragmentInteractionListener songsCallBack;
OnFragmentInteractionListener songsItemClick;
public interface OnFragmentInteractionListener {
public void onFragmentSetSongs(ArrayList<Song> s);
public void onSongListItemClick(int position);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
songsCallBack = (OnFragmentInteractionListener) getActivity();
songsItemClick = (OnFragmentInteractionListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onDetach() {
super.onDetach();
songsCallBack = null;
songsItemClick= null;
}
listView = (ListView) root.findViewById(R.id.listViewSongs);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
songsItemClick.onSongListItemClick(position);
}
});
,我做
songsCallBack.onFragmentSetSongs(songArrayList);
錯誤,當我點擊的ListView
at com..activities.MainActivity.onSongListItemClick(MainActivity.java:107)
at com..fragments.SongsListFragment$1.onItemClick(SongsListFragment.java:194)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1478)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3480)
錯誤位置
musicService.setSong(position); //Main Activity (MainActivity.java:107)
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
songsItemClick.onSongListItemClick(position); < -- here
}
請檢查我的編輯 –
一切似乎與回調良好。你檢查你的服務是否不爲空? –
哦,那是原因,我實際上沒有連接到服務。當我連接到onCreate上的服務,現在它已經修復。 –