• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ListView issue - it isn't finding my list for some reason

AlexGraal

Lurker
Hello everyone. First the code, then the error, and finally the explanation:

1. MainActivity.java
Code:
package com.example.addressbookapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {   

	Intent intent;
	TextView contactId;
	
	DBTools dbTools = new DBTools(this);

	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ArrayList<HashMap<String, String>> contactList = dbTools
				.getAllContacts();

		if (contactList.size() != 0) {
			ListView listView = getListView();
			listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					contactId = (TextView) view.findViewById(R.id.contactId);

					String contactIdValue = contactId.getText().toString();

					Intent theIntent = new Intent(getApplication(),
							EditContact.class);
					theIntent.putExtra("contactId", contactIdValue);

					startActivity(theIntent);

				}
			});

			ListAdapter adapter = new SimpleAdapter(MainActivity.this,
					contactList, R.layout.contact_entry, new String[] {
							"contactId", "lastName", "firstName" }, new int[] {
							R.id.contactId, R.id.lastName, R.id.firstName});
			
			setListAdapter(adapter);
		}

	}
	
	public void showAddContact(View view) {
		
		Intent theIntent = new Intent(getApplication(), NewContact.class);
		startActivity(theIntent);
		
	}


}

2. activity_main.xml
Code:
<TableLayout 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=".MainActivity" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000" >

        <TextView
            android:id="@+id/contactsTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/padding_5dp"
            android:layout_weight="1"
            android:text="@string/contacts_title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#FFF" />

        <Button
            android:id="@+id/button1"
            android:background="#444444"
            android:onClick="showAddContact"
            android:text="@string/add_button"
            android:textColor="#FFF"
            android:textSize="20sp" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
        </ListView>
    </TableRow>

</TableLayout>

3. Error message shown by logcat
Code:
07-09 07:03:09.774: I/Process(2068): Sending signal. PID: 2068 SIG: 9
07-09 07:03:29.435: D/AndroidRuntime(2088): Shutting down VM
07-09 07:03:29.485: W/dalvikvm(2088): threadid=1: thread exiting with uncaught exception (group=0xb0c90b20)
07-09 07:03:29.525: E/AndroidRuntime(2088): FATAL EXCEPTION: main
07-09 07:03:29.525: E/AndroidRuntime(2088): Process: com.example.addressbookapp, PID: 2088
07-09 07:03:29.525: E/AndroidRuntime(2088): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.addressbookapp/com.example.addressbookapp.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.os.Handler.dispatchMessage(Handler.java:102)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.os.Looper.loop(Looper.java:136)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.ActivityThread.main(ActivityThread.java:5017)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at java.lang.reflect.Method.invoke(Method.java:515)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at dalvik.system.NativeStart.main(Native Method)
07-09 07:03:29.525: E/AndroidRuntime(2088): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.ListActivity.onContentChanged(ListActivity.java:243)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.Activity.setContentView(Activity.java:1929)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at com.example.addressbookapp.MainActivity.onCreate(MainActivity.java:27)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.Activity.performCreate(Activity.java:5231)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-09 07:03:29.525: E/AndroidRuntime(2088): 	... 11 more


What the heck is going on here? As far as I understand, it isn't find a listview whose ID attribute is 'android.R.id.list', but there obviously is one. Here is an excerpt from R.java:
Code:
        public static final int lastName=0x7f050046;
        public static final int lastNameTextView=0x7f050045;
        public static final int list=0x7f050040;
        public static final int listMode=0x7f050001;
        public static final int list_item=0x7f050029;
As you can see, "list" is right there.

What's wrong?

PS - I also posted this same exact thread on java-forums.org, as I'm active there. However, I'm sure how quickly their community will be able to answer my question as they aren't very android oriented, so I posted it here as well.

I'm looking forward to getting a response!

Thanks,
Alex
 
Back
Top Bottom