[英]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.