Reputation: 1
I dont know why the App. crashs when i try to display the number of selected rows inside a cursor. when I comment on the line that displays the number of selected rows the App. works fine otherwise it crashs.
Here is the code:
@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
Upvotes: 1
Views: 716
Reputation: 8304
The problem is with Toast.makeText.
makeText()
is an overloaded method which comes in two flavors -
makeText(Context c, String s, int duration)
and
makeText(Context, int resId, int duration)
What you're using is the second one (the one with resId) - since cursor.getCount returns an integer. Now android is trying to find (in your Resources class @gen folder) which String is the value of cursor.getCount affiliated with. Since it can't find one, it throws a Resources$NotFoundException.
fix: instead of cursor.getCount()
, try (String.valueOf(cursor.getCount()))
Upvotes: 1
Reputation: 38168
How does it crash ? You have to find out the details of your error. ow can you understand problems you don't precisely understand ?
Look at your logcat details and find the culprit and the type of the error.
Chances are :
------- UPDATE -----
Your problem is that you pass an int to Toast. It then expects the int to represent a resourceId.
If you had passed a string, you wouldn't have had this problem. Use
Toast.makeText(getBaseContext(), String.valueOf( c.getCount() ), toast.LENGTH_SHORT).show();
Upvotes: 0