我正在尝试创建一个应用程序,该应用程序需要用户输入主题名称和子代码,并且该子代码将用作字段的名称来创建数据库Table.But我得到一个即使正在保存共享首选项,创建数据库时也会出现空指针异常。
这不是A" context.getSharedPreference问题"请看一下。
public class Check_regularHelper extends SQLiteOpenHelper {
Context mcontext=null;
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Check_regular.db";
SharedPreferences pref;
private String createCMD(){
String s = "";
int total=pref.getInt("sno",0);
for(int i=1;i<=total;i++){
s += pref.getString(String.valueOf(i),"") + " INTEGER DEFAULT 0";
if(i!=total){
s+=", ";
}
}
return s;
}
public String SQL_CREATE_ENTRIES = "CREATE TABLE "+ SubContract.Check_Regular.TABLE_NAME
+ " (" + SubContract.Check_Regular.DATE + " TEXT PRIMARY KEY," + createCMD() +");";
public Check_regularHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mcontext=context;
this.pref = context.getSharedPreferences(subjectFill.Subjects,Context.MODE_PRIVATE);
if(pref==null) Log.e(context.toString(),"fine-----------------");
if(pref!=null) Log.e(context.toString(),"wrong-----------------");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
08-24 19:21:06.184 8226-8226/com.creator.innov.check_regular E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.creator.innov.check_regular, PID: 8226
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.creator.innov.check_regular/com.creator.innov.check_regular.DailyTimeTable.Check_regularSubject}: java.lang.NullPointerException: Attempt to invoke interface method 'int android.content.SharedPreferences.getInt(java.lang.String, int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
即使我试图检查sharedPreferences是否为null,它也没有显示任何东西。请帮忙
答案 0 :(得分:1)
在初始化pref之前评估赋值字符串SQL_CREATE_ENTRIES,这就是pref为空的原因。
尝试这种方式:
public String SQL_CREATE_ENTRIES ;
public Check_regularHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mcontext=context;
this.pref = context.getSharedPreferences(subjectFill.Subjects,Context.MODE_PRIVATE);
if(pref==null) Log.e(context.toString(),"fine-----------------");
if(pref!=null) {
Log.e(context.toString(),"wrong-----------------");
SQL_CREATE_ENTRIES = "CREATE TABLE "+ SubContract.Check_Regular.TABLE_NAME
+ " (" + SubContract.Check_Regular.DATE + " TEXT PRIMARY KEY," + createCMD() +");";
}
}
答案 1 :(得分:-1)
尝试使用app context来获取共享首选项:
public class App extends Application {
public static Application application;
@Override
public void onCreate() {
super.onCreate();
application = this;
}
public static Application get(){
return application;
}
}
将Manifest添加到应用程序标签android:name =“。App”
public class Helper extends SQLiteOpenHelper {
private static final String DB_NAME = "sample.db";
private static final int VERSION = 1;
private String CREATE =
"CREATE TABLE " + "%s"
+ "("
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "%s TEXT,"
+ "%s TEXT"
+ ");";
public Helper(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
Context appContext = App.get();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext);
String tableName = sharedPreferences.getString("TABLE_NAME", "TABLE1");
String column1 = sharedPreferences.getString("COLUMN1", "COLUMN1");
String column2 = sharedPreferences.getString("COLUMN2", "COLUMN2");
String formatted = String.format(CREATE, tableName, column1, column2);
sqLiteDatabase.execSQL(formatted);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}