用于创建数据库的类:
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class sqlite extends Activity {
public void sqlitetry(final SQLiteDatabase sqLiteDB) {
String createSql = "CREATE TABLE" +
"(_id integer primary key autoincrement, " +
"name text not null);";
sqLiteDB.execSQL(createSql);
}
public void sqlinit() {
SQLiteDatabase db = openOrCreateDatabase("MyDatabase",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE id note exist MyTable(username Varchar, password Varchar PRIMARY KEY);");
Cursor c = db.rawQuery("Select * from MyTable", null);
c.moveToFirst();
while(c.isAfterLast() == false) {
}
}
}
当我将此代码添加到我的登录和注册确认类并启动活动时,它会强制关闭:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
sqlite as = new sqlite();
SQLiteDatabase sqLiteDB = null;
as.sqlitetry(sqLiteDB);
as.sqlinit();
在我的设备上安装 apk 后,它会显示在 logcat 中:
07-18 09:51:39.875: D/AndroidRuntime(25677): Shutting down VM
07-18 09:51:39.875: W/dalvikvm(25677): threadid=1: thread exiting with uncaught exception (group=0x40018578)
07-18 09:51:39.882: E/AndroidRuntime(25677): FATAL EXCEPTION: main
07-18 09:51:39.882: E/AndroidRuntime(25677): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login1/com.example.login1.Login}: java.lang.NullPointerException
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.os.Handler.dispatchMessage(Handler.java:99)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.os.Looper.loop(Looper.java:130)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.main(ActivityThread.java:3687)
07-18 09:51:39.882: E/AndroidRuntime(25677): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 09:51:39.882: E/AndroidRuntime(25677): at java.lang.reflect.Method.invoke(Method.java:507)
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
07-18 09:51:39.882: E/AndroidRuntime(25677): at dalvik.system.NativeStart.main(Native Method)
07-18 09:51:39.882: E/AndroidRuntime(25677): Caused by: java.lang.NullPointerException
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.example.login1.sqlite.sqlitetry(sqlite.java:12)
07-18 09:51:39.882: E/AndroidRuntime(25677): at com.example.login1.Login.onCreate(Login.java:25)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 09:51:39.882: E/AndroidRuntime(25677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-18 09:51:39.882: E/AndroidRuntime(25677): ... 11 more
我需要将用户注册时的信息放入数据库并将其拉出以便用户登录,但它强制关闭,我不知道该怎么办。谁能帮我?