Android ListView进入Tab

时间:2014-08-22 10:21:58

标签: android listview

我想将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" />

enter image description here

2 个答案:

答案 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