user2495899
user2495899

Reputation:

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

I have a listfragment class:

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. 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

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

but I am not sure then how to call that in my onCreateView method.

Thanks for all your help!

Upvotes: 1

Views: 120

Answers (2)

rasmeta
rasmeta

Reputation: 475

Yes. First of all you do have to change the ID of your ListView like so:

<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. This is because Android built-in resource reside in 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 :

@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 ;)

Upvotes: 1

Emmanuel
Emmanuel

Reputation: 13223

In onCreateView() you need to provide a layout that has a ListView with and id of @android:id/list.

ListFragment provides some helper methods (e.g setListAdapter()) that require the layout to have a ListView with that id so it can reference it and set the adapter for you.

Upvotes: 0

Related Questions