简体   繁体   English

ListFragment:运行时错误“您的内容必须具有ID属性为'android.R.list'的ListView

[英]ListFragment: Runtime error "Your content must have a ListView whose id attribute is 'android.R.list'

I have a listfragment class: 我有一个listfragment类:

public class activityfeedScreen extends ListFragment implements OnItemClickListener {

private ListView activityfeed_feedList_listView;
private ActivityFeedBaseAdapter adapter;

public static final activityfeedScreen newInstance()
{
    activityfeedScreen fragment = new activityfeedScreen();
    Bundle bdl = new Bundle(1);
    fragment.setArguments(bdl);
    return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);

    activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);
    activityfeed_feedList_listView.setAdapter(adapter);
    activityfeed_feedList_listView.setOnItemClickListener(this);

    return v;
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
public void onItemClick(AdapterView<?> activityfeedlistview, View view, int position, long itemID) {
    Intent intent_activityfeed_showitemdescription = new Intent(view.getContext(), activityfeedItemScreen.class);
    startActivity(intent_activityfeed_showitemdescription);
}
}

In this class I am getting an error at the line: 在此类中,我在行上遇到了一个错误:

    activityfeed_feedList_listView = (ListView)v.findViewById(R.id.activityfeed_feedList_listView);  

I initially thought it was a problem due to the getView() being null so I moved that from the onCreate method to the onCreateView() method. 最初,我认为这是一个问题,因为getView()为null,所以我将其从onCreate方法移到了onCreateView()方法。 But I am getting the runtime error as stated in the title. 但是我得到了标题中所述的运行时错误。 Here is my layout file: 这是我的布局文件:

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp">

    <ImageView
        android:id="@+id/activityfeed_profilePicture_imageView"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/default_profile"/>

</RelativeLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B9B9B9"
    android:layout_marginTop="20dp">

    <ListView
        android:id="@+id/activityfeed_feedList_listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        </ListView>
    </RelativeLayout>

I understand that I must change the listview id to 我了解我必须将listview ID更改为

android:id="@android:id/list"

but I am not sure then how to call that in my onCreateView method. 但是我不确定该如何在我的onCreateView方法中调用它。

Thanks for all your help! 感谢你的帮助!

Yes. 是。 First of all you do have to change the ID of your ListView like so: 首先,您必须像这样更改ListView的ID:

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

In your onCreateView you can reference the ID by android.R.id.list . 在您的onCreateView您可以通过android.R.id.list引用ID。 This is because Android built-in resource reside in android.R . 这是因为Android内置资源位于android.R You can also find there drawables, styles, etc. (you can call them in a similar maneer). 您还可以在其中找到可绘制对象,样式等(可以在类似样式中调用它们)。

Taking the above into consideration, your onCreateView method should look like this : 考虑到上述因素,您的onCreateView方法应如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_activityfeed_screen, container, false);

    activityfeed_feedList_listView = (ListView)v.findViewById(android.R.id.list);
    activityfeed_feedList_listView.setAdapter(adapter);
    activityfeed_feedList_listView.setOnItemClickListener(this);

    return v;
}

That's all ;) 就这样 ;)

In onCreateView() you need to provide a layout that has a ListView with and id of @android:id/list . onCreateView()您需要提供一个具有ListView且ID为@android:id/list的布局。

ListFragment provides some helper methods (eg setListAdapter() ) that require the layout to have a ListView with that id so it can reference it and set the adapter for you. ListFragment提供了一些帮助方法(例如setListAdapter() ),这些方法要求布局具有具有该ID的ListView ,以便它可以引用它并为您设置适配器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您的内容必须具有ID属性为“ android.R.id.list”错误的ListView - Your content must have a ListView whose id attribute is 'android.R.id.list' error Logcat错误内容必须具有ID属性为&#39;android.R.id.list&#39;的ListView - Logcat error-Content must have a ListView whose id attribute is 'android.R.id.list' Android:内容必须有一个 id 属性为 &#39;android.R.id.list 的 ListView - Android: Content must have a ListView whose id attribute is 'android.R.id.list 为什么我会收到“您的 TabHost 必须有一个 TabWidget,其 id 属性为‘android.R.id.tabs’”错误? - Why do I get an "Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'" error? 在ListFragment中找不到ListView&#39;android.R.id.list&#39; - ListView 'android.R.id.list' is not found in ListFragment Android必须具有ID为的Expandableview - Android must have Expandableview whose id is FATAL EXCEPTION:main java.lang.RuntimeException:Content具有id属性&#39;android.R.id.list&#39;的视图,它不是ListView类 - FATAL EXCEPTION: main java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class ListFragment内的ListView崩溃,尽管其中有一个@ id / list - ListView inside a ListFragment crashes although there is a @id/list in it 由于ListView id而导致运行时错误 - Runtime Error because of ListView id Android android.R.id.list ListView Fragment异常 - Android android.R.id.list ListView Fragment Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM