2

所以基本上,我正在尝试编写一个封装 SQLite 数据库的 ContentProvider。我在网上到处查看,遵循了几个不同的教程,并不断遇到同样的问题。我注意到很多人问同样的问题,但那里的建议都没有帮助我。

基本上,每当我在我的 SQLiteOpenHelper 上调用“getWriteableDatabase”时,它都会抛出一个空指针异常并且什么都没有创建。这是我的代码:

public class DatabaseProvider extends ContentProvider{

private static int DATABASE_VERSION = 1;

public static final String GRADE_COMPOSITION_TABLE = "gradecompositiontable";
public static final String GRADE = "Grade";
public static final String CLASS = "Class";
public static final String CLASS_CODE = "Class_Code";


public static final String ACTIVITY_CODE_TABLE = "activitycodetable";
//CLASS_CODE
public static final String ACTIVITY_CODE = "Activity_Code";

MainDatabaseHelper dbHelper;
public SQLiteDatabase dBase;

@Override
public Uri insert(Uri uri, ContentValues values)  throws SQLiteException{
    System.out.println("Helper: " + dbHelper);
    dBase = dbHelper.getWritableDatabase();  <<<Null exception here
    return null;
}

@Override
public boolean onCreate(){
    dbHelper = new MainDatabaseHelper(getContext());
    return (dbHelper == null)?false:true;
}


protected static final class MainDatabaseHelper extends SQLiteOpenHelper{

    public static String overallDBName = "TeachMEDatabase";

    private static final String SQL_CREATE_GRADE_COMP_TABLE = 
            "CREATE TABLE " +
            GRADE_COMPOSITION_TABLE +
            "(" +
            GRADE + " INTEGER, " +
            CLASS + " TEXT, " +
            CLASS_CODE + " INTEGER PRIMARY KEY)";


    public MainDatabaseHelper(Context context){
        super(context, overallDBName, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_GRADE_COMP_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.execSQL("DROP TABLE IF EXISTS " + GRADE_COMPOSITION_TABLE);
        onCreate(db);
    }

}

我从我的主要活动中创建了这个对象,一切正常,直到我调用“插入”,也就是它崩溃的时候。

编辑:这是堆栈跟踪

04-09 21:53:46.301: E/AndroidRuntime(3397): Uncaught handler: thread main exiting due to uncaught exception
04-09 21:53:46.307: E/AndroidRuntime(3397): java.lang.RuntimeException: Unable to start activity ComponentInfo{sioy.teachme/sioy.teachme.TeachMEFirstActivity}: java.lang.NullPointerException
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.os.Looper.loop(Looper.java:123)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.app.ActivityThread.main(ActivityThread.java:4363)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at java.lang.reflect.Method.invoke(Method.java:521)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at dalvik.system.NativeStart.main(Native Method)
04-09 21:53:46.307: E/AndroidRuntime(3397): Caused by: java.lang.NullPointerException
04-09 21:53:46.307: E/AndroidRuntime(3397):     at sioy.teachme.data.DatabaseProvider.insert(DatabaseProvider.java:54)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at sioy.teachme.TeachMEFirstActivity.onCreate(TeachMEFirstActivity.java:15)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-09 21:53:46.307: E/AndroidRuntime(3397):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-09 21:53:46.307: E/AndroidRuntime(3397):     ... 11 more
4

0 回答 0