我不知道为什么要使用该应用程序。当我尝试显示光标内的选定行数时发生崩溃。当我在显示应用程序所选行数的行上进行评论时。工作正常,否则会崩溃。
这是代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dbd = new DBDemoActivity(this);
db = dbd.getWritableDatabase();
cv = new ContentValues();
cv.put("name", "Amr");
cv.put("lat", 10);
cv.put("lng", 20);
newID = db.insert("Demo", null, cv);
cv.put("name", "Mikila");
cv.put("lat", 14);
cv.put("lng", 22);
newID = db.insert("Demo", null, cv);
cv.put("name", "bakri");
cv.put("lat", 17);
cv.put("lng", 29);
newID = db.insert("Demo", null, cv);
cv.put("name", "geomatics");
cv.put("lat", 50);
cv.put("lng", 90);
newID = db.insert("Demo", null, cv);
}
SQLiteCursor c = (SQLiteCursor) db.rawQuery("SELECT * FROM Demo WHERE lat = 10", null);
//c.moveToFirst();
//Toast.makeText(getBaseContext(), c.getCount(), Toast.LENGTH_SHORT).show();
//dbd.deleteMP(1);
}
}
LogCat:
02-27 10:46:35.675: E/AndroidRuntime(25525): FATAL EXCEPTION: main
02-27 10:46:35.675: E/AndroidRuntime(25525): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbook.DBDemo/com.androidbook.DBDemo.DemoData}: android.content.res.Resources$NotFoundException: String resource ID #0x9
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.os.Looper.loop(Looper.java:123)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.app.ActivityThread.main(ActivityThread.java:3691)
02-27 10:46:35.675: E/AndroidRuntime(25525): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 10:46:35.675: E/AndroidRuntime(25525): at java.lang.reflect.Method.invoke(Method.java:507)
02-27 10:46:35.675: E/AndroidRuntime(25525): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
02-27 10:46:35.675: E/AndroidRuntime(25525): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
02-27 10:46:35.675: E/AndroidRuntime(25525): at dalvik.system.NativeStart.main(Native Method)
02-27 10:46:35.675: E/AndroidRuntime(25525): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x9
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.content.res.Resources.getText(Resources.java:222)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.widget.Toast.makeText(Toast.java:258)
02-27 10:46:35.675: E/AndroidRuntime(25525): at com.androidbook.DBDemo.DemoData.onCreate(DemoData.java:54)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-27 10:46:35.675: E/AndroidRuntime(25525): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-27 10:46:35.675: E/AndroidRuntime(25525): ... 11 more
最佳答案
问题在于Toast.makeText。makeText()
是一种重载方法,有两种形式-
makeText(Context c, String s, int duration)
和
makeText(Context, int resId, int duration)
您正在使用的是第二个(带有resId的第二个)-因为cursor.getCount返回一个整数。现在,android试图(在您的Resources类@gen文件夹中)找到哪个String是附属于cursor.getCount的值。由于找不到,因此将引发Resources $ NotFoundException。
修复:尝试使用(String.valueOf(cursor.getCount())代替
cursor.getCount()
关于android - 获取游标中的行数时出错?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9462848/