【问题标题】:displaying a Prepopulated database as a listview [duplicate]将预填充的数据库显示为列表视图 [重复]
【发布时间】:2014-12-03 02:19:32
【问题描述】:

我正在尝试在列表视图中显示数据库的多个列。我正在使用 sqliteasset 助手和 row.xml 和 main.xml。我的主要活动扩展了一个列表活动。我得到的错误是 findviewbyid 找不到带有“R.id.list”的线性布局,但我的 activity_main.xml 有它。

这是我在列表中显示数据库信息的主类。

public class MainActivity extends ListActivity {

    private Cursor schedule;
    private MyDatabase db;
    ListView listView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new MyDatabase(this);
        schedule = db.getSchedule();

        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(new SimpleCursorAdapter(
           this,  // The context
           R.layout.row,  // The layout of the row to show
           db.getSchedule(),  // Here the cursor where the data is
                               // but it can be anything
           new String[] {"fName", "fType" },  // here the tables to show
           new int[] { android.R.id.text1, android.R.id.text2 },  // here where to show? the IDs
                                             // of the layout elements where put data.
           0));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        schedule.close();
        db.close();
    }
}

行:

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        android:textColor="#0000FF"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="TextView"
        android:textColor="#0000FF"
        android:textSize="25dp" />

</LinearLayout>

Activity_Main:(可以看到id.list)

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

LogCat:

12-02 01:04:44.436: E/AndroidRuntime(1183): FATAL EXCEPTION: main
12-02 01:04:44.436: E/AndroidRuntime(1183): Process: com.example.mealplan, PID: 1183
12-02 01:04:44.436: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mealplan/com.example.mealplan.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.os.Looper.loop(Looper.java:136)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.main(ActivityThread.java:5017)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invoke(Method.java:515)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at dalvik.system.NativeStart.main(Native Method)
12-02 01:04:44.436: E/AndroidRuntime(1183): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Activity.setContentView(Activity.java:1929)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at com.example.mealplan.MainActivity.onCreate(MainActivity.java:35)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Activity.performCreate(Activity.java:5231)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-02 01:04:44.436: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

【问题讨论】:

标签: java android xml listview


【解决方案1】:

改变这一行:

android:id="@+id/list"

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

【讨论】:

  • 我的程序不再崩溃,它显示两个“文本视图”并排显示!下一步是让他们说出正确的话。为什么我需要手动更改?喜欢它有什么不同,所以我不会再犯这个错误了。
  • ListActivity 需要一个在 android R 类中定义的预定义 id。您使用 @+id 定义了一个额外的 id,ListActivity 无法将其识别为正确的 id。
【解决方案2】:

您正在尝试从 android 默认库中获取列表 ID。您应该从自己的布局中检索列表 ID。

改变这一行

ListView listView = (ListView) findViewById(android.R.id.list);

ListView listView = (ListView) findViewById(R.id.list);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多