2017-08-24 94 views
0

我想創建一個應用程序,它需要用戶輸入主題名稱和子代碼,並且該子代碼將用作字段名稱來創建數據庫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爲空或不是,它沒有表現出任何的事情。請幫助

回答

-1

嘗試使用的應用程序上下文來獲取共享偏好:

public class App extends Application { 

    public static Application application; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     application = this; 

    } 

    public static Application get(){ 
     return application; 
    } 
} 

添加在清單應用程序標記機器人:名字= 「應用程序」。

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) { 

       } 
      } 
+0

你是天才man.Thanks Man。 .. :) – Creatorcool

1

賦值字符串SQL_CREATE_ENTRIES在pref被初始化之前被賦值,這就是爲什麼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() +");"; 
      } 
    } 
+0

不,仍然存在錯誤...「由...引發:java.lang.NullPointerException:試圖調用接口方法int android.content.SharedPreferences.getIn t(java.lang.String,int)'空對象引用「 – Creatorcool

+0

然後你可以在檢查'preg'不是null之後進行賦值,就像我剛剛更新的代碼 – dmastra