1

用于创建数据库的类:

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

我需要将用户注册时的信息放入数据库并将其拉出以便用户登录,但它强制关闭,我不知道该怎么办。谁能帮我?

4

2 回答 2

2

根据logcat,你可以看到这行报告了实际的错误:

07-18 09:51:39.882: E/AndroidRuntime(25677): Caused by: java.lang.NullPointerException

在该行之后,您将看到 2 行是生成实际错误的代码执行堆栈:

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)

基于此,您的问题仅仅是因为 null value sqLiteDB

SQLiteDatabase sqLiteDB = null;
as.sqlitetry(sqLiteDB);
于 2013-07-18T02:15:07.807 回答
0

你的sqlite类扩展了 Activity。你也没有onCreate在 sqlite 中的方法

然后你有这个

 sqlite as = new sqlite(); 

您需要使用startActivity(new Intent(myintent)).

建议你学习活动。

http://developer.android.com/guide/components/activities.html

你也有这个

 sqlite as = new sqlite(); //sqlite class extends activity
 SQLiteDatabase sqLiteDB = null; // null value
 as.sqlitetry(sqLiteDB);
 as.sqlinit(); 

Activity如果您的 sqlite只是一个辅助类,则它不需要扩展。

另请查看本教程

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

查看教程 @ Youtube By travis

http://www.youtube.com/watch?v=gEg9OdufXmM(我猜是 112 到 119 系列)

于 2013-07-18T02:19:33.170 回答