致命异常:主要java.lang.RuntimeException的:内容有鉴于与id属性“android.R.id.list”这不是一个ListView类是一个、属性、这不、异常

2023-09-13 00:41:48 作者:国民萌叔

我试图用一个的ListView 在片段。但我得到这个错误:致命异常:主要java.lang.RuntimeException的:内容有鉴于与id属性android.R.id.list这不是一个ListView类。 我的code:

I'm trying to use a ListView in a fragment. But i get this Error: FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class. My Code:

我的ListFragment:

My ListFragment:

public class whitelist_list extends ListFragment {


    Context mContext;

    @Override
    public void onAttach(Activity activity) {
        mContext = activity;
        Log.i("Event", "onAttach called");
        super.onAttach(activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);
        return inflater.inflate(R.layout.whitelist_content, container, false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something with the data
    }

}

whitelist_list newFragment = new whitelist_list();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, newFragment)
.commit();

编辑: 解决方法:声明列表视图与Android:ID =@ ID / Android的:清单

Solution: Declare Listview with android:id="@id/android:list"

推荐答案

如果您使用的是ListActivity /片段,在你的布局ID为您的ListView应该是@android:ID /列表,因此:  在您的whitelist_list.xml(whatever_activityname.xml)在你的ListView改变ID到Android:ID =@机器人:ID /列表

If you use a ListActivity/Fragment, the id for your ListView in your layout should be @android:id/list, so: In your whitelist_list.xml (whatever_activityname.xml) in your ListView change the id to android:id="@android:id/list"

<ListView android:id="@android:id/list"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>