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

时间:2015-07-16 08:48:04

标签: android-sqlite android-cursor

我试图向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.access$800(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.access$800(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引起的,但是字符串存在且拼写清楚。

如何保存字符串?

1 个答案:

答案 0 :(得分:1)

您的上下文可能是空的。
尝试使用function obj(name, lname){ this.name = name; this.lastName = lname; } Object.defineProperty(obj.prototype, 'fullName', { get : function() { return this.name + " " + this.lastName; } }); var aObj = new obj("first", 'lastN'); console.log(aObj.fullName);代替ES2015