2011-03-23 25 views
0

我在這裏有另一個類似於這個帖子,但是,我已經對我的類進行了一些更改,現在有一個不同的問題。前一個問題也從未解決。SQLiteOpenHelper空指針異常

我的應用程序需要能夠從2個不同的事件寫入android上的sqlite3數據庫。我的一件事是寫入數據庫就好了。當第二個事件嘗試寫入數據庫時​​發生附加錯誤。

我無法解決此問題。我已經看了一個多星期了。任何幫助是極大的讚賞。如果需要其他信息,請告訴我!只要我能解決這個問題,我就會發布我所擁有的一切,這會強調我。

The Error Logcat 
03-22 23:50:27.065: INFO/System.out(281): Where: DB-submitData 
03-22 23:50:30.846: WARN/System.err(281): java.lang.NullPointerException 
03-22 23:50:30.865: WARN/System.err(281):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 
03-22 23:50:30.865: WARN/System.err(281):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98) 
03-22 23:50:30.875: WARN/System.err(281):  at cpe495.smartapp.SmartDBHelper.open(SmartDBHelper.java:70) 
03-22 23:50:30.875: WARN/System.err(281):  at cpe495.smartapp.DataBuilder.submitData(DataBuilder.java:37) 
03-22 23:50:30.884: WARN/System.err(281):  at cpe495.smartapp.DataBuilder.prepareData(DataBuilder.java:29) 
03-22 23:50:30.904: WARN/System.err(281):  at cpe495.smartapp.SmartApp$2.dataAnalyzedReceived(SmartApp.java:56) 
03-22 23:50:30.904: WARN/System.err(281):  at cpe495.smartapp.DataRobot.fireDataAnalyzedEvent(DataRobot.java:269) 
03-22 23:50:30.916: WARN/System.err(281):  at cpe495.smartapp.DataRobot.analyzeData(DataRobot.java:79) 
03-22 23:50:30.925: WARN/System.err(281):  at cpe495.smartapp.SmartApp$1.dataReceivedReceived(SmartApp.java:49) 
03-22 23:50:30.935: WARN/System.err(281):  at cpe495.smartapp.ConnectDevice.fireDataReceivedEvent(ConnectDevice.java:79) 
03-22 23:50:30.945: WARN/System.err(281):  at cpe495.smartapp.ConnectDevice.run(ConnectDevice.java:46) 
03-22 23:50:30.945: WARN/System.err(281):  at java.lang.Thread.run(Thread.java:1096) 

//The main class SmartApp.java 
public class SmartApp extends Activity implements OnSharedPreferenceChangeListener { 
    TextView smartConnectionStatus; 
    TextView testOutputView; 
    Thread cThread; 
    private ConnectDevice cD = new ConnectDevice(); 
    private DataRobot dR = new DataRobot(this); 
    private DataBuilder dB = new DataBuilder(); 
    private DataSender dS = new DataSender(this); 

    Handler mHandler = new Handler(); 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.intro); 

     cD.addDataReceivedListener(new DataReceivedListener() { 
      @Override 
      public void dataReceivedReceived(DataReceivedEvent event) { 
       // TODO Auto-generated method stub 
       dR.analyzeData(event.getData()); 
      } 
     }); 
     dR.addDataAnalyzedListener(new DataAnalyzedListener() { 
      @Override 
      public void dataAnalyzedReceived(DataAnalyzedEvent event) { 
       // TODO Auto-generated method stub 
       dB.prepareData(event.getData()); 
      } 
     }); 
     dR.addDataAlertListener(new DataAlertListener() { 
      @Override 
      public void dataAlertReceived(DataAlertEvent event) { 
       Log.v("SmartApp", "data alert event caught"); 
       DataAlert a = new DataAlert(SmartApp.this); 
       mHandler.post(a); 
      } 
     }); 
     dR.addDataNotifyListener(new DataNotifyListener() { 
      @Override 
      public void dataNotifyReceived(DataNotifyEvent event) { 
       // TODO Auto-generated method stub 
       Log.v("SmartApp", "data notification event caught"); 
       DataNotification a = new DataNotification(SmartApp.this); 
       mHandler.post(a); 
      } 
     }); 
     dB.addDataBuilderListener(new DataBuilderListener() { 
      @Override 
      public void dataBuilderReceived(DataBuilderEvent event) { 
       // TODO Auto-generated method stub 
       dS.sendData(event.getData()); 
      } 
     }); 
    } 
    private Context getContext() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

//The DataBuilder.java class that fails to access the database 
public class DataBuilder extends Activity { 
    private List _listeners = new ArrayList(); 
    private SmartDataObject data; 
    SmartDBHelper sDBH = new SmartDBHelper(this); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.v("databuilder", "on create"); 
    } 

    public void prepareData(SmartDataObject temp) { 
     submitData(temp); 
    } 

    public void submitData(SmartDataObject temp) { 
     data = temp; 
     System.out.println("Where: DB-submitData"); 
     try { 
      sDBH.open(); 
      sDBH.insertDataResponse(data.getHeartRate(), data.getAct(), data.getTimeStamp()); 
      sDBH.close(); 
      fireDataBuilderEvent(data); 
     } 
     catch(SQLException e) { 
      e.printStackTrace(); 
     } 
     catch(NullPointerException e) { 
      e.printStackTrace(); 
     } 
    } 
    public synchronized void addDataBuilderListener(DataBuilderListener listener) { 
     _listeners.add(listener); 
    } 
    public synchronized void removeDataBuilderListener(DataBuilderListener listener) { 
     _listeners.remove(listener); 
    } 
    private synchronized void fireDataBuilderEvent(SmartDataObject temp) { 
     DataBuilderEvent dRE = new DataBuilderEvent(this, temp); 
     Iterator listeners = _listeners.iterator(); 
     while(listeners.hasNext()) { 
      ((DataBuilderListener)listeners.next()).dataBuilderReceived(dRE); 
     } 
    } 
    public interface DataBuilderListener { 
     public void dataBuilderReceived(DataBuilderEvent event); 
    } 
} 

//The DataNotificationSurvey.java class that access the database successfully. 
public class DataNotificationSurvey extends Activity { 
    private Date timeStamp; 
    private Uri mUri; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.datanotificationlayout); 
     Log.v("datanotificationsurvey", "inside datanotificationsurvey"); 


     timeStamp = new Date(DataNotification.when); 

     TextView notifyDate = (TextView) findViewById(R.id.notifyDateTV); 
     notifyDate.setText(timeStamp.toLocaleString()); 
     final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton); 
     final RadioButton patientCrisisRB = (RadioButton) findViewById(R.id.patientCrisis); 
     final RadioButton physicalActivityRB = (RadioButton) findViewById(R.id.physicalActivity); 

     notifySubmitButton.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         if(patientCrisisRB.isChecked()) { 
          submitNotify(1, timeStamp); 
         } 
         else if(physicalActivityRB.isChecked()) { 
          submitNotify(2, timeStamp); 
         } 
         finish(); 
        } 
       }); 
    } 

    public void submitNotify(int tempType, Date tempDate) { 
     SmartDBHelper sDBH = new SmartDBHelper(this); 
     sDBH.open(); 
     sDBH.insertNotificationResponse(tempType, tempDate); 
     sDBH.close(); 
     /*ContentValues values = new ContentValues(); 
     values.put("userresponse", tempType); 
     values.put("notifytime", (tempDate.getTime()/1000)); 
     mUri = getContentResolver().insert(intent.getData(), values);*/ 
    } 
} 

//The SQLiteOpenHelper class, it extends this DatabaseHelper. 
public class SmartDBHelper extends Activity { 

    private DatabaseHelper dBH; 
    private SQLiteDatabase db; 
    private final Context mCtx; 

    private static final String DATABASE_NAME = "smart_lite_db.db"; 
    private static final int DATABASE_VERSION = 2; 
    private static final String NOTIFY_TABLE_NAME = "user_notify_data"; 
    private static final String HR_TABLE_NAME = "user_hr_data"; 
    private static final String NOTIFY_TABLE_CREATE = 
     "CREATE TABLE " + NOTIFY_TABLE_NAME + 
     " (counter INTEGER PRIMARY KEY, " + 
     "userresponse INTEGER, " + 
     "notifytime INTEGER);"; 
    private static final String DATA_TABLE_CREATE = 
     "CREATE TABLE " + HR_TABLE_NAME + 
     " (counter INTEGER PRIMARY KEY, " + 
     "hr INTEGER, " + 
     "act INTEGER, " + 
     "timestamp INTEGER);"; 

    static class DatabaseHelper extends SQLiteOpenHelper { 

     DatabaseHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 
      Log.v("smartdbhelper", "before creation"); 
      db.execSQL(NOTIFY_TABLE_CREATE); 
      Log.v("smartdbhelper", "middle creation"); 
      db.execSQL(DATA_TABLE_CREATE); 
      Log.v("smartdbhelper", "after creation"); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 

     } 
    } 

    public SmartDBHelper(Context context) { 
     this.mCtx = context; 
    } 

    public SmartDBHelper open() throws SQLException { 
     dBH = new DatabaseHelper(mCtx); 
     db = dBH.getWritableDatabase(); 
     return this; 
    } 

    public void close() { 
     dBH.close(); 
    } 

    public long insertNotificationResponse(int tempType, Date tempDate) { 
     ContentValues values = new ContentValues(); 
     values.put("userresponse", tempType); 
     values.put("notifytime", (tempDate.getTime()/1000)); 
     return db.insert(NOTIFY_TABLE_NAME, null, values); 
    } 

    public long insertDataResponse(double tempAct, int tempHR, long tempDate) { 
     ContentValues values = new ContentValues(); 
     values.put("hr", tempHR); 
     values.put("act", tempAct); 
     values.put("timestamp", (tempDate/1000)); 
     return db.insert(HR_TABLE_NAME, null, values); 
    } 
} 
+1

不接受實際上不是答案的答案。你強迫人們閱讀評論列表,發現其實並不是答案。 – Dave 2012-07-20 15:30:52

+1

投票結束。因爲它太本地化,不會幫助未來的訪問者。 – prolink007 2012-07-20 18:29:41

回答

2

這是一個老問題,但其接受的答案是錯的,筆者不能刪除整個事情,所以這裏未來遊客着想就是答案。

在DataBuilder中,您在傳遞Context之前已將其初始化爲SmartDBHelper的構造函數。

SmartDBHelper sDBH = new SmartDBHelper(this); 

您必須等到onCreate()訪問一個有效的上下文:

public class DataBuilder extends Activity { 
    private List _listeners = new ArrayList(); 
    private SmartDataObject data; 
    SmartDBHelper sDBH; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.v("databuilder", "on create"); 
     sDBH = new SmartDBHelper(this); 
    } 

    // Rest of class 
} 

此外,我不相信DataBuilder或SmartDBHelper應該擴展活動。他們不使用任何活動的方法,並且無法通過致電new DataBulder()開始活動:

public class DataBuidler { 

public class SmartDBHelper { 
0

您DatabaseHelper類定義爲靜態的。這是造成所有問題的原因。當嵌套類被聲明爲靜態它沒有封閉類

更多詳細信息的讀取訪問實例變量和方法Nested Class in Java

+0

當DatabaseHelper類不是靜態的時,我得到相同的結果。我已經測試過,我改爲靜態,因爲有人建議我在幾天前嘗試。 – prolink007 2011-03-23 14:53:18

+0

在這種情況下,我不確定。你可以在調用getWritableDatabase方法之前檢查dBH是否爲空 – Josnidhin 2011-03-24 03:45:27

+0

我已經測試過這個,每次我做出修改時都會說修復問題。上面列出的方式,在調用getWritableDatabase之前,dBH不爲null。但是其他的實現它是空的。 – prolink007 2011-03-24 14:49:00

1

SQLiteOpenHelper.java:98是分貝= mContext.openOrCreateDatabase(MNA​​ME,0,mFactory );

讓你在SQLiteOpenHelper設置一個空的上下文

+0

這就是對的。這是我的情況和在這裏相同的問題:http://stackoverflow.com/questions/13183234/getwritabledatabase-throwing-null-pointer-exception – 2013-10-24 17:07:36