Search code examples
androidlistviewandroid-listviewr.java-file

Android complaining about content not having Listview with id android.R.id.List despite having that


obviously not a profound question, but I think theres probably an error in my approach.

So I've added the file listview.xml to my project in the layout folder.

it contains this:

<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="@+id/android:list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" />

I've also tried:

<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" />

And i my main activity function generally looks like this:

public class ToDoManagerActivity extends ListActivity {

private static final int ADD_TODO_ITEM_REQUEST = 0;
private static final String FILE_NAME = "TodoManagerActivityData.txt";
private static final String TAG = "Lab-UserInterface";

// IDs for menu items
private static final int MENU_DELETE = Menu.FIRST;
private static final int MENU_DUMP = Menu.FIRST + 1;

ToDoListAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a new TodoListAdapter for this ListActivity's ListView
    mAdapter = new ToDoListAdapter(getApplicationContext());

    // Put divider between ToDoItems and FooterView
    getListView().setFooterDividersEnabled(true);

    // TODO - Inflate footerView for footer_view.xml file         
    TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, null);

    // NOTE: You can remove this block once you've implemented the assignment
    if (null == footerView) {
        return;
    }
    // TODO - Add footerView to ListView
    getListView().addFooterView(footerView);
    setListAdapter(mAdapter);

    setContentView(R.layout.footer_view);

This keeps breaking with the error:

...java.lang.RunTimeException: content must have a ListView whose id attribute is 'android.R.id.list'

I noticed that if I comment out the setContentView(R.layout.footer_view) bit the error disappears, which is quite confusing as well, because it seems like the error should trigger before there.

This is confusing the hell out of me because as far as I can tell this element exists. Its seems like maybe I'm missing a step to load the ListView? I'm banging my head against the wall here and this seems like something really basic, and being a n00b sucks...So any help is much appreciated!

Cheers!

EDIT:

footer_view.xml contents:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:text="@string/add_new_todo_item_string"
    android:textSize="24sp" >

</TextView>

EDIT2:

Current onCreate after suggested edits:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Create a new TodoListAdapter for this ListActivity's ListView
    mAdapter = new ToDoListAdapter(getApplicationContext());

    // Put divider between ToDoItems and FooterView
    getListView().setFooterDividersEnabled(true);

    setListAdapter(mAdapter);
    TextView footerView = (TextView) getLayoutInflater().inflate(R.layout.footer_view, getListView(), false);
    getListView().addFooterView(footerView);
}

Solution

  • For an Activity that is extending ListActivity, the ListView needs to exist within the xml file that you are using in the activity, in your case that's the footer_view file.

    Taken from the Google Dev Site - "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list""

    To incorporate your footer below your list, try this:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <TextView
            android:id="@+id/footerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/add_new_todo_item_string"
            android:textSize="24sp" >
    
        </TextView>
    </LinearLayout>