0

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

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>

这是我的日志文件:

01-16 01:01:14.845: E/AndroidRuntime(806): FATAL EXCEPTION: main
01-16 01:01:14.845: E/AndroidRuntime(806): java.lang.RuntimeException: Unable to start activity ComponentInfo{alertme.app/alertme.app.AlertViews}: java.lang.NullPointerException
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.os.Looper.loop(Looper.java:137)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-16 01:01:14.845: E/AndroidRuntime(806):  at java.lang.reflect.Method.invokeNative(Native Method)
01-16 01:01:14.845: E/AndroidRuntime(806):  at java.lang.reflect.Method.invoke(Method.java:511)
01-16 01:01:14.845: E/AndroidRuntime(806):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-16 01:01:14.845: E/AndroidRuntime(806):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-16 01:01:14.845: E/AndroidRuntime(806):  at dalvik.system.NativeStart.main(Native Method)
01-16 01:01:14.845: E/AndroidRuntime(806): Caused by: java.lang.NullPointerException
01-16 01:01:14.845: E/AndroidRuntime(806):  at alertme.app.AlertViews.onCreate(AlertViews.java:21)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.Activity.performCreate(Activity.java:5008)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-16 01:01:14.845: E/AndroidRuntime(806):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

有任何想法吗?

4

1 回答 1

2

您正在尝试使用您的变量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));
于 2013-01-15T19:44:26.930 回答