我试图使用SQLite。由于这是我第一次尝试实现SQLite,因此我遵循this tutorial
但是我收到了这样的错误
08-21 20:47:44.129: E/AndroidRuntime(1710): FATAL EXCEPTION: main
08-21 20:47:44.129: E/AndroidRuntime(1710): Process: com.indomultimedia.hellobali, PID: 1710
08-21 20:47:44.129: E/AndroidRuntime(1710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.indomultimedia.hellobali/com.indomultimedia.hellobali.MainHelloballi}: java.lang.NumberFormatException: Invalid int: "null"
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.ActivityThread.access$800(ActivityThread.java:163)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.os.Handler.dispatchMessage(Handler.java:102)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.os.Looper.loop(Looper.java:157)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.ActivityThread.main(ActivityThread.java:5335)
08-21 20:47:44.129: E/AndroidRuntime(1710): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 20:47:44.129: E/AndroidRuntime(1710): at java.lang.reflect.Method.invoke(Method.java:515)
08-21 20:47:44.129: E/AndroidRuntime(1710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-21 20:47:44.129: E/AndroidRuntime(1710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-21 20:47:44.129: E/AndroidRuntime(1710): at dalvik.system.NativeStart.main(Native Method)
08-21 20:47:44.129: E/AndroidRuntime(1710): Caused by: java.lang.NumberFormatException: Invalid int: "null"
08-21 20:47:44.129: E/AndroidRuntime(1710): at java.lang.Integer.invalidInt(Integer.java:137)
08-21 20:47:44.129: E/AndroidRuntime(1710): at java.lang.Integer.parseInt(Integer.java:354)
08-21 20:47:44.129: E/AndroidRuntime(1710): at java.lang.Integer.parseInt(Integer.java:331)
08-21 20:47:44.129: E/AndroidRuntime(1710): at com.indomultimedia.hellobali.DatabaseHandler.getAllContacts(DatabaseHandler.java:98)
08-21 20:47:44.129: E/AndroidRuntime(1710): at com.indomultimedia.hellobali.MainHelloballi.onCreate(MainHelloballi.java:177)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.Activity.performCreate(Activity.java:5389)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-21 20:47:44.129: E/AndroidRuntime(1710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
08-21 20:47:44.129: E/AndroidRuntime(1710): ... 11 more
这是我的代码上的错误行。
MainHellobali.java
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<AllItem> allItem = db.getAllContacts(); //Error in this line
for (AllItem cn : allItem) {
String log = "Id: "+cn.getTableID()+" ,Name: " + cn.getCategory_name() + " ,Phone: " + cn.getItem_name();
// Writing Contacts to log
Log.d("Name: ", log);
}
DatabaseHandler.java
// Getting All Contacts
public List<AllItem> getAllContacts() {
List<AllItem> contactList = new ArrayList<AllItem>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ALLITEM;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
AllItem allitem = new AllItem();
allitem.setTableID(Integer.parseInt(cursor.getString(0))); // error in this line
allitem.setCategory_name(cursor.getString(1));
allitem.setItem_Name(cursor.getString(2));
// Adding contact to list
contactList.add(allitem);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
我不知道错误显示的原因。我尝试尽可能地遵循教程。 任何人都可以帮忙修复此错误吗?
答案 0 :(得分:3)
Integer.parseInt(cursor.getString(0))
这是触发错误的行。
cursor.getString(0)
返回null
。总的来说,我建议您用以下内容替换此行:cursor.getInt(0)
。但是,如果列为空,我仍然会抛出这个,我想。