Question

ListView is not detected in Custom.Class when bridging from custom.xml file ..i have done some silly mistake please correct me..

public class Custom extends Activity {

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom);
        ListView lv=(ListView)findViewById(R.id.list); // list is not detected..showing error
    }
}

XML custom

<?xml version="1.0" encoding="utf-8"?>
<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="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>
Was it helpful?

Solution

Replace

<ListView
    android:id="@id/android:list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

with

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

Whenever you want to give id to a View you have to use android:id="@+id/your_id

OTHER TIPS

android:id="@+id/list" use the notation @+ to add new id

Change the following -- android:id="@+id/list"

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

Now following will detect the list- ListView lv=(ListView)findViewById(R.id.list);

If you want to refer Android resources , which are already defined in Android system, with @android:id/.. while to access resources that you have defined/created in your project, you use @id/.

Check this SO -- Difference between "@id/" and "@+id/" in Android

So for your case, you can also define list view by android:id="@android:id/list"
Also you can check this so Android: id list view

Using @id/android:list is for referencing the default list in a ListActivity.

You could change your Activity to a ListActivity and reference the ListView in your activity code with the getListView() method. For example:

public class Custom extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom);
        ListView lv = getListView();
    }
}

Alternatively as others have suggested you could use:

android:id="@+id/list"

Instead of:

android:id="@id/android:list"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top