我想将我的SQL Server数据库与android SQLite同步。
我已按照链接http://programmerguru.com/android-tutorial/how-to-sync-remote-mysql-db-to-sqlite-on-android/
上提供的教程进行操作不同之处在于我使用Microsoft Visual Studio Web Service代替PHP。
我已复制代码并根据我的要求进行了更改。现在,每当我运行移动应用程序时,我都会收到以下错误:
无法启动活动ComponentInfo {com.murcyleenpeerzada.mp/com.murcyleenpeerzada.mp.writings}:java.lang.NullPointerException:尝试调用虚方法'java.util.ArrayList com.murcyleenpeerzada.mp.DBHandler.getAllWriters ()'在空对象引用
上
我检查了它有记录的表格。该表不是空白。
我在DBHandler中的代码是:
public ArrayList<HashMap<String, String>> getAllWriters() {
ArrayList<HashMap<String, String>> usersList;
usersList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM " + MP_TABLE;
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("writeId", cursor.getString(0));
map.put("title", cursor.getString(1));
usersList.add(map);
} while (cursor.moveToNext());
}
database.close();
return usersList;
}
我的活动代码是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.writings);
// Get User records from SQLite DB
ArrayList<HashMap<String, String>> userList = dbHandler.getAllWriters();
// If users exists in SQLite DB
if (userList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), userList, R.layout.writings_controls, new String[] {
"userId", "userName" }, new int[] { R.id.writings_Title, R.id.writings_Des});
ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
}