【问题标题】:Android ListActivity Fatal ExceptionAndroid ListActivity 致命异常
【发布时间】:2013-01-31 02:21:34
【问题描述】:

每当我尝试开始此活动时都会收到此错误:

01-31 02:19:19.558: E/AndroidRuntime(1318): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prva/com.example.prva.ListView}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

列表活动:

package com.example.prva;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;


public class ListView extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        fillData();
        }

    private void fillData() {
        // Get all of the notes from the database and create the item list
        Cursor c = DatabaseManager.getAllData();
        startManagingCursor(c);

        String[] from = new String[] { DatabaseManager.TABLE_COLUMN_ONE };
        int[] to = new int[] { R.id.text1 };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
        setListAdapter(notes);
    }
    }

我的列表视图 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="326dp" >
    </ListView>

</LinearLayout>

我不明白这一点,因为我有一个 id 是列表的列表视图?

【问题讨论】:

    标签: java android listview exception listactivity


    【解决方案1】:

    将您的 ListView id 更改为 @android:id/list 这将解决您的问题..List Activity 应该具有带有 @android:id/list 的列表视图..我希望这会对您有所帮助

    【讨论】:

      【解决方案2】:

      您必须使用默认的android.R.id.list,它位于@android:id 名称下。 @+id 创建您的自己的 ID,名称为list

      所以,而不是:

      android:id="@+id/list"
      

      使用以下内容:

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

      【讨论】:

        猜你喜欢
        • 2012-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-02
        • 2012-09-08
        • 2015-03-01
        相关资源
        最近更新 更多