我想将ListView
附加到Tabs extends ListFragment中。运行项目并选择选项卡后,我收到此错误:
Unfortiunately, TSMS has stopped
logCat结果:
08-22 04:23:00.974 1867-1867/ir.tsms E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
<{1>} onCreateView
MyFriendsListFragment
函数中的我有此代码附加Tab
:
onTabSelected
课程中的 MainActivity
:
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// mViewPager.setCurrentItem(tab.getPosition());
switch ( tab.getPosition() ){
case 1:
MyFriendsListFragment simpleFriendsList = new MyFriendsListFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.drawer, simpleFriendsList).commit();
break;
}
}
onCreateView
类中的 MyFriendsListFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity()
.getBaseContext(), R.layout.list_fragment,
new String[] {
"AAAAAAA",
"BBBBBBB",
"CCCCCCC",
"DDDDDDD"
});
setListAdapter(adapter);
/*return super.onCreateView(inflater, container, savedInstanceState);*/
/*return inflater.inflate(R.layout.list_fragment, container, false);*/
}
list_fragment.xml:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/list"
android:layout_height="match_parent"
android:background="#dddd"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
答案 0 :(得分:2)
android:id =“@ + id / list”与android.R.id.list不同“@ + id / list”在你的包中创建一个新的id。 正确的id应该是android:id =“@ android:id / list”。
答案 1 :(得分:1)
更改此
android:id="@+id/list"
到
android:id="@android:id/list"
此外,您使用setListAdapter
表示您的片段将以ListFragment
延伸。
同时改变这一点。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getActivity()
.getBaseContext(), R.layout.urCustomLayout,
new String[] {
"AAAAAAA",
"BBBBBBB",
"CCCCCCC",
"DDDDDDD"
});
setListAdapter(adapter);
return inflater.inflate(R.layout.list_fragment, container, false);
}
您需要将TextView
的ID传递给ArrayAdapter
。如果您使用自定义布局,则传递textview ID
的ID,如果您使用默认的Android布局,则应使用android.R.id.textView1
。