我想創建一個應用程序,它需要用戶輸入主題名稱和子代碼,並且該子代碼將用作字段名稱來創建數據庫Table.But在創建時收到空指針異常數據庫甚至共享首選項正在被保存。如何通過從共享首選項獲取字段來創建數據庫?
這不是一個「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爲空或不是,它沒有表現出任何的事情。請幫助
你是天才man.Thanks Man。 .. :) – Creatorcool