Results 1 to 4 of 4
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

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

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

    1. MainActivity.java
    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
    Java 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
    Java 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:
    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?

    P.S. - Since people's knowledge on here may be limited, I posted the exact same thread over at AndroidForums.com.
    Last edited by AlexGraal; 07-09-2014 at 01:23 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: ListView issue - it isn't finding my list for some reason

    See the answer here: Your content must have a ListView whose id attribute is &#39;android.R.id.list&#39; - Stack Overflow

    Rename the id of your ListView like this,

    <ListView android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
    Since you are using ListActivity your xml file must specify the keyword android while mentioning to a ID.

    If you need a custom ListView then instead of Extending a ListActivity, you have to simply extend an Activity and should have the same id without the keyword android.
    This was the first result for googling your error, btw. Also, it would be handy if you could link between the crossposts, that way people on one forum can get help from answers on the other, and we don't waste our time repeating help you've already received.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: ListView issue - it isn't finding my list for some reason

    Can you explain why? Why does the naming for the listview have to be done that way? What's the difference between the two?


    The link to my other post:
    ListView issue - it isn't finding my list for some reason - Android Forums

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: ListView issue - it isn't finding my list for some reason

    Quote Originally Posted by AlexGraal View Post
    Can you explain why? Why does the naming for the listview have to be done that way? What's the difference between the two?
    Because that's what ListView is looking for. It's looking for an exact ID that includes the Android stuff. If you want to use a different ID (or multiple IDs) then you should extend Activity instead of ListActivity.

    I didn't know any of this beforehand, by the way. I'm getting all this just by googling your error. You'd be surprised what a google search will tell you.


    Quote Originally Posted by AlexGraal View Post
    Well you might want to link to this question from that question, that way people over there don't waste their time answering a question you already have an answer for.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 3
    Last Post: 04-14-2011, 06:59 AM
  2. Finding the closest point in a list of points
    By sAntA199 in forum New To Java
    Replies: 8
    Last Post: 12-13-2009, 08:41 AM
  3. Replies: 2
    Last Post: 12-10-2009, 12:06 PM
  4. Finding the most repeated names in a list
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-17-2009, 03:08 PM
  5. Finding objects in a list
    By starwars in forum AWT / Swing
    Replies: 5
    Last Post: 09-11-2009, 03:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •