0

1-我想使用 ListFragment。

2 - 我的类从 Activity 扩展而不是 ListActivity(因为我想在 ListView 的顶部有一个 TextView)。因此,这是主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
            android:id="@+id/txt"
            android:layout_width="fill_parent"
            android:layout_height="40sp" />

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这是我的代码:

public class List1 extends SherlockListFragment {

    private ArrayList<String> list = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("onCreate............");

        for(int i=0; i<30; i++)
            list.add("item " + i);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        System.out.println("onCreateView.................");

        View view = inflater.inflate(R.layout.main_list1, container, false);

        TextView label = (TextView) view.findViewById(R.id.txt);
        label.setText("Hello");

        ListView lv = (ListView) view.findViewById(R.id.listView);
          lv.setAdapter(new MultiAdapter(view.getContext()));

        return view;
    }

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("onActivityCreated..........");


    }

    private class MultiAdapter extends ArrayAdapter<String> {

        public MultiAdapter(Context context) {
            super(context, R.layout.list1, list);
        }

        @Override
        // Called when updating the ListView
        public View getView(int position, View convertView, ViewGroup parent) {

            View row;

            if (convertView == null)  {
                LayoutInflater inflater = getActivity().getLayoutInflater();
                row = inflater.inflate(R.layout.list1, parent, false);
            } else
                row = convertView;

            TextView txt = (TextView) row.findViewById(R.id.label);
            txt.setText(list.get(position));    

            return row;
        }
    }

当我使用 Tab 转到此 Fragment 时,应用程序崩溃了。这是我在日志中得到的例外:

01-01 14:59:51.154: E/AndroidRuntime(20091): FATAL EXCEPTION: main
01-01 14:59:51.154: E/AndroidRuntime(20091): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.support.v4.app.ListFragment.ensureList(ListFragment.java:344)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.support.v4.app.ListFragment.onViewCreated(ListFragment.java:145)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:431)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at com.example.tabtest.FragmentTab$TabManager.onTabChanged(FragmentTab.java:134)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:359)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.widget.TabHost.setCurrentTab(TabHost.java:344)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:458)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.view.View.performClick(View.java:2533)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.view.View$PerformClick.run(View.java:9320)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.os.Handler.handleCallback(Handler.java:587)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.os.Looper.loop(Looper.java:150)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at android.app.ActivityThread.main(ActivityThread.java:4385)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at java.lang.reflect.Method.invokeNative(Native Method)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at java.lang.reflect.Method.invoke(Method.java:507)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
01-01 14:59:51.154: E/AndroidRuntime(20091):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

您的片段布局必须有一个ListView带有 id: 的android.R.id.list,否则 android 将不知道要使用哪个 ListView。

像这样:

<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

注意android:id="@android:id/list".

于 2013-01-01T14:20:48.283 回答