-1

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.

Prabindh
  • 3,356
  • 2
  • 23
  • 25
user609511
  • 4,091
  • 12
  • 54
  • 86

4 Answers4

2

Change this

android:id="@+id/TheClient"

to

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

There is no need to ListView Cast it because already extends your Activity with ListActivity

and also remove

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

and directly set adapter by using

setListAdapter(new CustomListAdapter(this, results));
M D
  • 47,665
  • 9
  • 93
  • 114
1

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.

Kartik_Koro
  • 1,277
  • 1
  • 11
  • 23
0

One potential error will come from the fact that you use upper case in your id

android:id="@+id/TheClient"

try with lower case

android:id="@+id/the_client"
Tr4X
  • 309
  • 1
  • 8
0

Rename the id of your ListView

 android:id="@+id/TheClient"

to

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

Since you are using ListActivity your xml file must specify the keyword android while mentioning to a ID

and remove

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

from activity code.

i.e. simply rewrite your code as

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



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

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


        dbcon.close();

    }
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74