没有从getWritableDatabase调用sqlite oncreate

时间:2013-09-20 17:38:08

标签: android sqlite

我已阅读过很多有关此事的帖子,但他们都没有帮助过我。我正在扩展sqliteOpenHelper,当我调用getWriteableDatabase时,我得到一个空指针异常。我把日志语句放在onCreate中,它们永远不会出现。我在每次运行后从手机上卸载了应用程序,试图强制onCreate,但它永远不会执行。我甚至更改了代码中的db名称以强制使用新的onCreate而不执行任何操作。我从片段中调用处理程序。我已经尝试发送主机活动上下文,以及可能是一个问题但没有帮助的应用程序上下文思考。这是代码:

public NewsDatabaseHelper(Context context) {
    super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    Log.i("DATABASE", "DATABASE CREATED");

        String CREATE_NEWS_TABLE = "CREATE TABLE " + TABLE_NEWS + "(" + COLUMN_DBID +
                " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TITLE + " TEXT," + COLUMN_AUTHOR + " TEXT " + 
                COLUMN_LINK + " TEXT " + COLUMN_PUBLISHDATE + " TEXT " + COLUMN_SNIPPET + " TEXT " + 
                COLUMN_CONTENT + " TEXT " + COLUMN_CATEGORY + " TEXT " + COLUMN_ID + " TEXT " + COLUMN_SECTIONNAME + " TEXT " + 
                COLUMN_TRAILTEXT + " TEXT " + COLUMN_LASTMOD + " TEXT " + COLUMN_THUMBNAIL + ")";
        db.execSQL(CREATE_NEWS_TABLE);
        Log.i("DATABASE", "DATABASE CREATED");
    /*// create the newsitems table
    db.execSQL("create table newsitems (_id integer primary key autoincrement, " +
            "title varchar(100), author varchar(100), link varchar(100), pubdate varchar(50) snippet varchar(100), content varchar(1000), " +
            "categories varchar(100), sectionid varchar(100), trailtext varchar(100), lastmod varchar(100), thumbnail varchar(100))"    );*/

}

public long insertNewsItem(SingleNewsItem news){

long dbId = -1;
boolean updating = false;



    ContentValues cv = new ContentValues();


    cv.put(COLUMN_AUTHOR, news.getAuthor());
    cv.put(COLUMN_CATEGORY, news.getCategories());
    cv.put(COLUMN_CONTENT, news.getContent());
    cv.put(COLUMN_ID, news.getId());
    cv.put(COLUMN_LASTMOD, news.getLastModified());
    cv.put(COLUMN_LINK, news.getLink());
    cv.put(COLUMN_PUBLISHDATE, news.getPublishedDate());
    cv.put(COLUMN_SECTIONNAME, news.getSectionName());
    cv.put(COLUMN_SNIPPET, news.getContentSnippet());
    cv.put(COLUMN_THUMBNAIL, news.getThumbnail());
    cv.put(COLUMN_TITLE, news.getTitle());
    cv.put(COLUMN_TRAILTEXT, news.getTrailText());

        return getWritableDatabase().insert(TABLE_NEWS, null, cv);



}

以下是我实例化类的片段中的代码:

 NewsDatabaseHelper db = new NewsDatabaseHelper( ApplicationContext.getInstance());

 db.insertNewsItem(item);

最后,这是logcat。注意没有DATABASE CREATED ENTRY:

09-20 13:26:52.370: D/AndroidRuntime(19166): Shutting down VM
09-20 13:26:52.370: W/dalvikvm(19166): threadid=1: thread exiting with uncaught  exception (group=0x418db438)
09-20 13:26:52.370: E/AndroidRuntime(19166): FATAL EXCEPTION: main
09-20 13:26:52.370: E/AndroidRuntime(19166): java.lang.NullPointerException
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at com.gilchrist.android.googlenews.NewsDatabaseHelper.insertNewsItem(NewsDatabaseHelper.java:104)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at com.gilchrist.android.googlenews.GoogleNewsMainFragment.parseGuardianJSONObject(GoogleNewsMainFragment.java:360)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at com.gilchrist.android.googlenews.GoogleNewsMainFragment.access$1(GoogleNewsMainFragment.java:277)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at com.gilchrist.android.googlenews.GoogleNewsMainFragment$FetchItemsTask.onPostExecute(GoogleNewsMainFragment.java:191)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at com.gilchrist.android.googlenews.GoogleNewsMainFragment$FetchItemsTask.onPostExecute(GoogleNewsMainFragment.java:1)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.os.AsyncTask.finish(AsyncTask.java:631)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.os.Looper.loop(Looper.java:137)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at android.app.ActivityThread.main(ActivityThread.java:5021)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at java.lang.reflect.Method.invokeNative(Native Method) 
09-20 13:26:52.370: E/AndroidRuntime(19166):    at java.lang.reflect.Method.invoke(Method.java:511)
 09-20 13:26:52.370: E/AndroidRuntime(19166):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
09-20 13:26:52.370: E/AndroidRuntime(19166):    at dalvik.system.NativeStart.main(Native Method)

感谢。

0 个答案:

没有答案