编辑2:片段适配器:
public class PageAdapter extends FragmentStatePagerAdapter {
ArrayList<Song> songList; //Update
public PageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
return MusicList.newInstance(songList); //Update
}
return null;
}
@Override
public int getCount() {
return 1; //No of Tabs
}
}
编辑: id.music_list_fragment(layout.fragment_main)布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/music_list_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/headphones">
<ListView
android:id="@+id/song_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true">
</ListView>
</LinearLayout>
我正在尝试通过替换片段将活动“MainActivity”中的数组列表发送到其片段“MusicList”。
在活动中使用它:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = MusicList.newInstance(songList);
ft.replace(R.id.music_list_fragment, fragment);
ft.commit();
在片段中使用它:
public static MusicList newInstance(ArrayList<Song> songList) {
MusicList fragment = new MusicList();
Bundle bundle = new Bundle();
bundle.putSerializable(DESCRIBABLE_KEY, songList);
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_main, container, false);
mList = ((ListView)v.findViewById(R.id.song_list));
msongList = (ArrayList<Song>) getArguments().getSerializable(DESCRIBABLE_KEY);
SongAdapter sAdapter = new SongAdapter(getActivity(), msongList);
mList.setAdapter(sAdapter);
return v;
}
我收到了这个错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{pal.mplayercomeon/pal.mplayercomeon.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0c0074 (pal.mplayercomeon:id/music_list_fragment) for fragment MusicList{ee972d3 #0 id=0x7f0c0074}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0c0074 (pal.mplayercomeon:id/music_list_fragment) for fragment MusicList{ee972d3 #0 id=0x7f0c0074}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6681)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:1)
newInstance
方法之前,需要初始化ArrayList,否则为null。 getCount
方法加载ViewPager中的许多片段。 ft.replace(R.id.music_list_fragment, fragment);
,除非{strong 1}}作为 FrameLayout 包含在活动中 XML,而不是Fragment XML。但是,如上所述,您似乎想要使用ViewPager 附加点 - Song需要实现android:id="@+id/music_list_fragment"
,但是,您确实应该使用Serializable
,因为这是在Android中传递数据的首选方式。
答案 1 :(得分:0)
您尝试通过从活动中调用片段的linearlayout视图ID来从活动中替换片段内的视图。
您需要将<FrameLayout>
添加到您的活动xml
<FrameLayout android:id="@+id/fragment_placeholder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这将保留你的片段,直到你打电话:
ft.replace(R.id.fragment_placeholder, fragment);