我在這裏有另一個類似於這個帖子,但是,我已經對我的類進行了一些更改,現在有一個不同的問題。前一個問題也從未解決。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);
}
}
不接受實際上不是答案的答案。你強迫人們閱讀評論列表,發現其實並不是答案。 – Dave 2012-07-20 15:30:52
投票結束。因爲它太本地化,不會幫助未來的訪問者。 – prolink007 2012-07-20 18:29:41