0

私が得ているこのエラーを手伝ってくれるかどうか疑問に思っていました...

ソースコード:

Main.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;
    }
        // [...]
}

LogCat

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);

null コンテキストを渡しました。

に変更します

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 [...]

    }

あなたが参照している 2 行はどのメソッドの外側にもあり、実行されることはありません。

于 2013-03-09T23:45:05.267 に答える