我从服务器JSON
获取值并存储到本地数据库中。我收到了JSON
数组,我在logcat消息中看到了这个,但是当我习惯将值存储到本地数据库中时,我收到了错误。
public class DataBaseHandler extends SQLiteOpenHelper {
public DataBaseHandler(Context context, String name, CursorFactory factory,
int version) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
context = this.context;
}
Context context ;
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "BeaconDetails";
// Contacts table name
//table 1
private static final String TABLE_BEACON = "beacon_details";
// Contacts Table Columns names
//table 1
private static final String KEY_UUID = "uuid";
private static final String KEY_NAME = "name";
private static final String KEY_NOTIFICATION = "notification";
private static final String KEY_TYPE = "type";
private static final String KEY_MAJOR_ID = "major";
private static final String KEY_MINOR_ID = "minor";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE_BEACON = "CREATE TABLE " + TABLE_BEACON + "("
+ KEY_NAME + " TEXT,"+ KEY_UUID + " INTEGER PRIMARY KEY,"+ KEY_MAJOR_ID + " INTEGER PRIMARY KEY,"+ KEY_MINOR_ID + " INTEGER PRIMARY KEY"+ KEY_NOTIFICATION + " TEXT,"+ KEY_TYPE + " TEXT)";
db.execSQL(CREATE_TABLE_BEACON);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_BEACON);
// Create tables again
onCreate(db);
}
public long insertBeaconDetails(BeaconDetails beacondetailstore)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME,beacondetailstore.name);
values.put(KEY_UUID,beacondetailstore.uuid);
values.put(KEY_MAJOR_ID,beacondetailstore.major);
values.put(KEY_MINOR_ID,beacondetailstore.minor);
values.put(KEY_NOTIFICATION,beacondetailstore.notification);
values.put(KEY_TYPE,beacondetailstore.type);
long rowId = db.replace(TABLE_BEACON, null, values);
db.close();
return rowId;
}
MainActivity.java:
@Override
protected Void doInBackground(Void... voids) {
try {
String jsonString = getJsonString();
Log.v(TAG, "json: " + jsonString);
JSONArray jsonRegions = new JSONArray(jsonString);
for (int i = 0; i < jsonRegions.length(); i++) {
JSONObject jsonRegion = jsonRegions.getJSONObject(i);
beacondetailstore = new BeaconDetails();
beacondetailstore.name = jsonRegion.getString("name");
beacondetailstore.uuid = jsonRegion.getString("uuid");
beacondetailstore.major = jsonRegion.getInt("major_id");
beacondetailstore.minor = jsonRegion.getInt("minor_id");
beacondetailstore.notification=jsonRegion.getString("notification");
beacondetailstore.type = jsonRegion.getString("type");
db.insertBeaconDetails(beacondetailstore);
}
} catch (JSONException e) {
e.printStackTrace();
Log.d("myTag", e.getMessage());
}
记录值:
11-15 11:11:02.280: E/AndroidRuntime(15702): FATAL EXCEPTION: AsyncTask #1
11-15 11:11:02.280: E/AndroidRuntime(15702): java.lang.RuntimeException: An error occured while executing doInBackground()
11-15 11:11:02.280: E/AndroidRuntime(15702): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-15 11:11:02.280: E/AndroidRuntime(15702): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-15 11:11:02.280: E/AndroidRuntime(15702): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-15 11:11:02.280: E/AndroidRuntime(15702): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-15 11:11:02.280: E/AndroidRuntime(15702): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-15 11:11:02.280: E/AndroidRuntime(15702): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-15 11:11:02.280: E/AndroidRuntime(15702): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-15 11:11:02.280: E/AndroidRuntime(15702): at java.lang.Thread.run(Thread.java:841)
11-15 11:11:02.280: E/AndroidRuntime(15702): Caused by: java.lang.NullPointerException
11-15 11:11:02.280: E/AndroidRuntime(15702): at com.estimote.examples.demos.NotifyDemoActivity$JsonTask.doInBackground(NotifyDemoActivity.java:141)
11-15 11:11:02.280: E/AndroidRuntime(15702): at com.estimote.examples.demos.NotifyDemoActivity$JsonTask.doInBackground(NotifyDemoActivity.java:1)
11-15 11:11:02.280: E/AndroidRuntime(15702): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-15 11:11:02.280: E/AndroidRuntime(15702): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-15 11:11:02.280: E/AndroidRuntime(15702): ... 4 more
140th
行是db.insertBeaconDetails(beacondetailstore);
答案 0 :(得分:0)
错误有两种选择和替代解决方案。
1)问题是由于你的错误Context
。
2)您尚未初始化db
课程。所以你需要在onCreate()
方法上初始化它。
db = new DataBaseHandler(this, "BeaconDetails", null, 1)