class extends ListActivity “其 id 属性为“android.R.id.list””

发布于 2025-01-07 04:35:17 字数 2111 浏览 0 评论 0原文

我试图将我的主 Activity 扩展到“ListActivity”(以前扩展到:Activity)以使用 onListItemClick,但是 logcat 向我发送“您的内容必须有一个 id 属性为 'android.r.id.list' 的 ListView ’”。 (我使用 SQLite 来填充 ListView)。

mi xml 是:

<ListView
    android:id="@+id/list" // to "@id/android:list" or other styles (but nothing works)
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/> // or closing with </ListView>

这是 ListView 的完整 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" /></LinearLayout>

我处理 ListView 的主类:

public class lay_main extends ListActivity{
public ListView list;
private DBHelper myAdap;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lay_main);

    collectXML();
    setupDataBase();
    setupAdapter();

}      
private void collectXML() 
{
    list = (ListView)findViewById(R.id.list);
}

private void setupDataBase() {
    myAdap = new DBHelper(getApplicationContext(), "courses", null, 1);
    myAdap.insertCourses();

}

private void setupAdapter()
{
if(myAdap.getCourses()!=null)
    {
    cAdapter adapter = new  cAdapter(this, R.layout.list_courses, myAdap.getCourses());
    list.setAdapter(adapter);
    }

}}

我读了 Android:您的内容必须有一个 id 属性为 android.R.id.list 的 ListView,或:RuntimeException:您的内容必须具有 id 属性为的 ListView 'android.R.id.list',以及其他问题,但对我不起作用,这是怎么回事?

非常感谢你的帮助

I'm trying to extend my main Activity to "ListActivity" (previously extends to: Activity) to use onListItemClick, but the logcat, send me "Your content must-have a ListView whose id attribute is 'android.r.id.list'". (i'm using SQLite for populate the ListView).

mi xml is:

<ListView
    android:id="@+id/list" // to "@id/android:list" or other styles (but nothing works)
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/> // or closing with </ListView>

this is the complete xml for the ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" /></LinearLayout>

My main class to handle the ListView:

public class lay_main extends ListActivity{
public ListView list;
private DBHelper myAdap;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lay_main);

    collectXML();
    setupDataBase();
    setupAdapter();

}      
private void collectXML() 
{
    list = (ListView)findViewById(R.id.list);
}

private void setupDataBase() {
    myAdap = new DBHelper(getApplicationContext(), "courses", null, 1);
    myAdap.insertCourses();

}

private void setupAdapter()
{
if(myAdap.getCourses()!=null)
    {
    cAdapter adapter = new  cAdapter(this, R.layout.list_courses, myAdap.getCourses());
    list.setAdapter(adapter);
    }

}}

I read Android: Your content must have a ListView whose id attribute is android.R.id.list, or: RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list', and other questions, but don't works for me, what's going on here?

really would appreciate your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-01-14 04:35:17

当您想扩展 ListActivity 时,您应该使用它

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

,您将获得 ListView 作为

ListView listview = this.getListView();

ListView listview = (ListView)findViewById(android.R.id.list);
//consider android.R prefix

以下是详细说明:如何在Activity中使用getListView()?

When you want to extend ListActivity you should use

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

and you will get your ListView as

ListView listview = this.getListView();

or

ListView listview = (ListView)findViewById(android.R.id.list);
//consider android.R prefix

Here's the detailed explanation: How to use getListView() in Activity?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文