0

我想知道你是否可以帮助我解决我遇到的这个错误......

源代码:

主.java:

public class Main extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = this;

        DBAdapter dbA = new DBAdapter(context);
        dbA.open();    // error at this statement
            // [....]
    }
}

DBAdapter.java

public class DBAdapter {
    Context context;

    DatabaseHelper DBHelper;
    SQLiteDatabase db;

    public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    private static final class DatabaseHelper extends SQLiteOpenHelper {
        private static Context context;

        DatabaseHelper(Context ctx) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            context = ctx;
        }

        onCreate(SQLiteDatabase db) //  this line of code is never reached [...]
        // {...}
        onUpgradeonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) // neither is this one [...]
        // {...}

    }

    // ---opens the database---
    public DBAdapter open() throws SQLException {
        db = DBHelper.getWritableDatabase();  // error at this statement
        return this;
    }
        // [...]
}

日志猫

03-10 00:09:15.061: E/AndroidRuntime(5668): FATAL EXCEPTION: main
03-10 00:09:15.061: E/AndroidRuntime(5668): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akiand.wnb/com.akiand.wnb.main.Main}: java.lang.NullPointerException
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.ActivityThread.access$600(ActivityThread.java:142)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.os.Looper.loop(Looper.java:137)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.ActivityThread.main(ActivityThread.java:4931)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at java.lang.reflect.Method.invokeNative(Native Method)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at java.lang.reflect.Method.invoke(Method.java:511)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at dalvik.system.NativeStart.main(Native Method)
03-10 00:09:15.061: E/AndroidRuntime(5668): Caused by: java.lang.NullPointerException
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at com.akiand.wnb.db.DBAdapter.open(DBAdapter.java:179)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at com.akiand.wnb.main.Main.onCreate(Main.java:76)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.Activity.performCreate(Activity.java:5008)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
03-10 00:09:15.061: E/AndroidRuntime(5668):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139)
03-10 00:09:15.061: E/AndroidRuntime(5668):     ... 11 more

我找不到从 SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224) 弹出错误的任何参考

非常感激您的帮忙!

4

2 回答 2

2

我认为错误在

super(context, DATABASE_NAME, null, DATABASE_VERSION);

你传递了空上下文。

将其更改为

super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
于 2013-03-09T23:57:01.300 回答
1

查看右大括号:

 private static final class DatabaseHelper extends SQLiteOpenHelper {
    private static Context context;

    DatabaseHelper(Context ctx) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        context = ctx;
    }

            onCreate // this line of code is never reached [...]
            onUpgrade // neither is this one [...]

    }

您引用的两行在任何方法之外,永远不会被执行。

于 2013-03-09T23:45:05.267 回答