【问题标题】:ListView 'android.R.id.list' is not found in ListFragment在 ListFragment 中找不到 ListView 'android.R.id.list'
【发布时间】:2016-03-06 18:25:28
【问题描述】:

我收到了这个错误。

进程:com.example.user.timey,PID:17619
java.lang.RuntimeException: 你的内容必须有一个 ID 的 ListView 属性是'android.R.id.list'

我已经在 xml 的列表视图中定义了 android:id="@android:id/list"。 这是我的 xml 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.user.timey.OneFragment">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:divider="#E6E6E6"
    android:dividerHeight="1dp" >
</ListView>

</RelativeLayout>

这是我的java代码:

public class OneFragment extends ListFragment {

View rootView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    DBController controller = new DBController(getActivity());
    ArrayList<HashMap<String, String>> scheduleList = controller.getAllSchedules();
    if (scheduleList.size()!=0){
        ListView lv = getListView();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), scheduleList, R.layout.view_schedule_entry, new String[] { "scheduleId",
                "scheduleName", "scheduleStartTime" }, new int[] {
                R.id.scheduleId, R.id.scheduleName, R.id.scheduleTime });
        setListAdapter(adapter);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.fragment_one, container, false);
    return rootView;
}

有人知道我的代码有什么问题吗?非常感谢。

【问题讨论】:

    标签: java android xml listview


    【解决方案1】:

    在你的布局中像这样为 ListView 添加 id -

    android:id="@+id/list"
    

    不要在@android:id/list 等布局中为您的视图分配 id。

    然后在您的onCreateView() 方法代码中获取对它的引用 -

    ListView lv = (ListView) rootView.findViewById(R.id.list);
    

    【讨论】:

    • 再次阅读问题I have already defined android:id="@android:id/list" in my listview in xml. ?
    • 我知道。查看他添加ID的方式。我们为自己的视图添加了@+id 标签。
    • 竖起大拇指,你应该强调一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多