Java Android无法获取布局

Java Android无法获取布局,java,android,android-layout,android-activity,android-listview,Java,Android,Android Layout,Android Activity,Android Listview,我是Android新手。当我运行项目时,出现以下错误: Your content must have a ListView whose id attribute is 'android.R.id.list' 这句话的意思是: setContentView(R.layout.activity_main); 我的活动\u main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

我是Android新手。当我运行项目时,出现以下错误:

Your content must have a ListView whose id attribute is 'android.R.id.list'
这句话的意思是:

 setContentView(R.layout.activity_main); 
我的活动\u 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>

谢谢。

一个潜在的错误是您在id中使用大写字母

android:id="@+id/TheClient"
用小写字母试试

android:id="@+id/the_client"

重命名ListView的id

 android:id="@+id/TheClient"

由于您使用的是
ListActivity
,因此xml文件在提到ID时必须指定关键字android

移除

final ListView lv1 = (ListView) findViewById(R.id.TheClient);
来自活动代码

i、 e.只需将代码重写为

 @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();

    }
改变这个

android:id="@+id/TheClient"
android:id="@+id/TheClient"

无需
ListView
强制转换它,因为已使用
ListActivity
扩展了您的
Activity

并删除

final ListView lv1 = (ListView) findViewById(R.id.TheClient);
并使用直接设置适配器

setListAdapter(new CustomListAdapter(this, results));

列表活动扩展活动时,无需再次定义列表。
ListActivity
自动在布局集中查找id为“list”的
ListView
。如果未找到id为“list”的
列表视图
,则会给出错误信息

所以改变

并从活动中删除列表的定义。(删除findViewById行)

或者只是扩展普通活动而不是ListActivity

final ListView lv1 = (ListView) findViewById(R.id.TheClient);
setListAdapter(new CustomListAdapter(this, results));
android:id="@+id/TheClient"
android:id="@android:id/list"