如果使用 getResources.getString() 添加字符串值,则向 Sqlite 添加一行会崩溃

Add a row to Sqlite crashes if use getResources.getString() to add string value

我正在尝试向 Sqlite 添加一行。一开始我是这样添加行的:(有效)

sqlDB.execSQL("INSERT INTO Family VALUES (" + index_family + ",'" + R.string.f1_pavimentos + "','" + (R.string.f0_descripcionPropamsa + "','" + R.drawable.color_pavimento + "');");

但这不是个好主意,因为它只保存了字符串的 de id,而我想要这个值。所以我决定这样做:

sqlDB.execSQL("INSERT INTO Family VALUES (" + index_family + ",'" + context.getResources().getString(R.string.f1_pavimentos )+ "','" + context.getResources().getString(R.string.f0_descripcionPropamsa )+ "','" + R.drawable.color_pavimento + "');");

添加 getResources.getString() 但它崩溃并向我发送此错误:

07-16 10:42:26.200    1746-1746/com.propamsa E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.propamsa, PID: 1746
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.propamsa/com.propamsa.listas.FamilyList}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
            at com.propamsa.basededatos.ProductDBHandler.insertInformation(ProductDBHandler.java:183)
            at com.propamsa.basededatos.ProductDBHandler.onCreate(ProductDBHandler.java:57)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at com.propamsa.basededatos.ProductDBHandler.getAllFamiliesCursor(ProductDBHandler.java:109)
            at com.propamsa.listas.FamilyList.onCreate(FamilyList.java:39)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2264)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5139)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)

并且可以看出是由NullPointer引起的,但是该字符串存在并且拼写正确。

如何保存它的字符串?

可能,您的 上下文 为空。
尝试使用 getApplicationContext() 而不是 context