致命异常:主要 java.lang.RuntimeException:内容具有 id 属性为“android.R.id.list"的视图,该视图不是 ListView 类 [英] FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class

查看:17
本文介绍了致命异常:主要 java.lang.RuntimeException:内容具有 id 属性为“android.R.id.list"的视图,该视图不是 ListView 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 片段 中使用 ListView.但是我得到这个错误:FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class.
我的代码:

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:

我的列表片段:

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:list"

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

推荐答案

如果你使用 ListActivity/Fragment,你的布局中 ListView 的 id 应该是 @android:id/list,所以:在 ListView 的 whitelist_list.xml (whatever_activityname.xml) 中,将 id 更改为 android:id="@android:id/list"

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"/>

这篇关于致命异常:主要 java.lang.RuntimeException:内容具有 id 属性为“android.R.id.list"的视图,该视图不是 ListView 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆