【问题标题】:Android app crashes with weird errorAndroid 应用程序因奇怪的错误而崩溃
【发布时间】:2013-01-10 12:19:35
【问题描述】:

我有一个启动 ListActivity 的按钮,但每次单击它时,应用程序都会崩溃并出现以下 LogCat 错误:

01-10 13:12:51.327: E/AndroidRuntime(2970): FATAL EXCEPTION: main
01-10 13:12:51.327: E/AndroidRuntime(2970): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.app/com.app.Settings}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

这是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:background="@color/background"
android:orientation="vertical"
android:padding="15dip" >

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

</LinearLayout>

这是Java代码:

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class Settings extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        ArrayList<String> listItems=new ArrayList<String>();
        listItems.add("foo");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                listItems);
            setListAdapter(adapter);
    }

}

我已经尝试将列表 ID 更改为 list,但如果我这样做,我会收到以下错误:

If I do this I get the following error: `Error: No resource found that matches the given name (at 'id' with value '@id/list').`

【问题讨论】:

    标签: android android-layout android-listview


    【解决方案1】:

    ListActivity 不需要您通过setContentView() 方法为其分配布局,如果您只想显示一个ListView ListActivity 默认包含一个ListView。

    如果您需要在ListActivity 中包含比 ListView 更多的 View,您仍然可以为您的 Activity 分配布局。在这种情况下,您的布局必须包含一个 ListView,其 android:id 属性设置为 @android:id/list

    因此,将您的列表 ID 更改为:

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

    【讨论】:

    • 如果我这样做,我会收到以下错误:Error: No resource found that matches the given name (at 'id' with value '@id/list').
    • @user1301428 android:id/list 而不是 id/list
    【解决方案2】:

    XML 中的这一行:

    android:id="@+id/listViewSettings"
    

    ...必须是这样的:

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

    为什么?好吧,ListActivity 要求您使用 id “android.R.id.list”,如果您希望它保持您的ListView(使用ListFragment 时同样适用)。

    【讨论】:

      【解决方案3】:

      只需更改您的 xml ListView-id

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

      在这种情况下,您的布局必须包含一个ListView,并将android:id 属性设置为@android:id/list

      【讨论】:

      • @user1301428 尝试使用@android:id/list。只需在您的ListView 中替换您的android:id="@+id/listViewSettings" -->android:id="@android:id/list"
      猜你喜欢
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多