Android 尝试在listview中显示数据时,应用程序将被强制关闭

Android 尝试在listview中显示数据时,应用程序将被强制关闭,android,database,android-layout,listview,cursor,Android,Database,Android Layout,Listview,Cursor,我试图在列表视图中显示数据库中的内容,但每次打开它时,它都会被强制关闭。这是我的密码: public class AlertViews extends Activity { private DatabaseHelper db; private SQLiteDatabase datab; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub

我试图在列表视图中显示数据库中的内容,但每次打开它时,它都会被强制关闭。这是我的密码:

public class AlertViews extends Activity {

private DatabaseHelper db;
private SQLiteDatabase datab;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewalerter);
    ListView listview = (ListView)findViewById(R.id.viewalert);

//  db.getReadableDatabase();
    Cursor c=datab.query(DatabaseHelper.DATABASE_TABLE, new String[]{DatabaseHelper.KEY_ALERT,DatabaseHelper.KEY_TRIGGER}, null, null,null,null,String.format("%s", DatabaseHelper.KEY_ALERT));
    String x=c.getColumnName(0);
    String y=c.getColumnName(1);
    String[] cl={x,y};
    int[] v={R.id.nameal,R.id.trigna};
    SimpleCursorAdapter simcr=new SimpleCursorAdapter(this,R.layout.simplerow,c,cl,v,0);
    listview.setAdapter(simcr);

}

}
以下是上述活动的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"
android:orientation="vertical" >

<ListView
android:id="@+id/viewalert"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>

<TextView
android:id="@+id/nameal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<TextView
android:id="@+id/trigna"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>

有什么想法吗?

您试图在不初始化变量的情况下使用变量
datab
。就在这里:

Cursor c=datab.query(DatabaseHelper.DATABASE_TABLE, new String[]{DatabaseHelper.KEY_ALERT,DatabaseHelper.KEY_TRIGGER}, null, null,null,null,String.format("%s", DatabaseHelper.KEY_ALERT));

哪一行是数字21,它显然是导致错误的原因。第21行是注释中的注释。我仍然找不到问题所在。我使用datab=db.getReadableDatabase()初始化它;但它仍然向我显示相同的错误,并说上面的初始化不正确您也没有初始化
db
Cursor c=datab.query(DatabaseHelper.DATABASE_TABLE, new String[]{DatabaseHelper.KEY_ALERT,DatabaseHelper.KEY_TRIGGER}, null, null,null,null,String.format("%s", DatabaseHelper.KEY_ALERT));