Search code examples
javaandroidandroid-layoutandroid-activityandroid-listview

Android can not get the layout


I am new to Android. when I run my project, I got the following error:

Your content must have a ListView whose id attribute is 'android.R.id.list'

it crush on this line:

 setContentView(R.layout.activity_main); 

my activity_main.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.soft8.sql_test.MainActivity" >

    <ListView
        android:id="@+id/TheClient"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</RelativeLayout>

and main class:

public class MainActivity extends ListActivity {

    private ArrayList results = new ArrayList();
    SQLController   dbcon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        final ListView lv1 = (ListView) findViewById(R.id.TheClient);


        dbcon = new SQLController(this);
        dbcon.open();

        dbcon.insertSomeItmes(); 
        results = (ArrayList) dbcon.ReadData();
        lv1.setAdapter(new CustomListAdapter(this, results));


        dbcon.close();

    }

Thank you.


Solution

  • When you extend your activity from ListActivity, you don't need to define your list again. The ListActivity automatically looks in the layout set for it for a ListView with id "list". If it does not find a ListView with id "list" it gives your error.

    So change

    android:id="@+id/TheClient"
    

    to

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

    and remove definition of your list from activity. (remove findViewById line)

    Or just extend normal Activity instead of ListActivity.