2014-11-17 117 views
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 
} 

回答

1

更改名稱的接口,使其從大寫字母開頭。然後在你的活動中添加這一行來聲明你的活動:public class FragmentActivity extends Activity implements PassArrayList,重寫接口方法。而在片段中添加以下內容:

PassArrayList mCallback; 


@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    mCallback = (PassArrayList) getActivity(); 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    mCallback = null; 
} 

和地方在你的代碼,當你想通過你的歌曲列表回活動調​​用的方法onArticleSelected()mCallback對象上,並通過這種方法ArrayList中。然後,這個數組列表將作爲您的活動中的方法onArticleSelected()的參數,您可以使用它來製作任何您喜歡的內容。但不要忘記在onDetach()掛鉤方法中取消鏈接到mCallback,以防止上下文泄漏

+0

請檢查我的編輯 –

+0

一切似乎與回調良好。你檢查你的服務是否不爲空? –

+0

哦,那是原因,我實際上沒有連接到服務。當我連接到onCreate上的服務,現在它已經修復。 –

相關問題