android listView.setAdapter SimpleCursorAdapter 错误

android listView.setAdapter error with SimpleCursorAdapter

我正在尝试通过游标检索数据,将游标传递给 SimpleCursorAdapter 并将适配器设置为 listView,从而将数据库 table 中的数据显示到 listView 上,如下所示. 由于 listAdapter.setAdapter(适配器)问题,应用程序崩溃。

-我确认聊天中有数据 table 我正在使用

-捕获到异常后,ex.getMessage 为空,但它指示行号为带有 listAdapter.setAdapter(适配器)的行号 代码:

  ChatsActivity.java
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    Conn connection = new Conn();
    SQLiteDatabase db = connection.getDB(this, "chats", 1);
    //returns readable database

    Cursor result = db.rawQuery("SELECT * FROM chats", null);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.other,
            result,
            new String[]{"_id", "lastContent"},
            new int[]{R.id.textView1, R.id.textView2});

        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        setContentView(listView);

    }
}  

2 Other.xml

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Main"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="Sub"
    android:textAppearance="?android:attr/textAppearanceMedium" />  </RelativeLayout>

Activity_chats.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ChatsActivity">


<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/listView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />  </RelativeLayout>

可能是什么问题?

这里:

setContentView(listView);

不是将 listView 传递给 setContentView,而是在从布局访问值之前将 Activity_chats.xml 布局 ID 作为 Activity 布局传递:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Activity_chats);
        ....
    }