Reputation: 322
I have a program where I use SQL to store the users credit. It used to work fine until one faithful day it just stopped running. Here is where the problem first occurs.
package com.inc.nicky.tapit;
MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
Product product =
dbHandler.findProduct("Coins");
eee=(TextView)findViewById(R.id.someID);
if (product != null) {
eee.setText(product.getQuantity());
} else {
product =
new Product("Coins", 0);
dbHandler.addProduct(product);
eee.setText("0");
}
LogCat says that it cannot change the Text to product.getQuantity(). However if I remove this line and get to a future usage of the SQL the problem still remains; I cannot call product.getQuantity() even when I am not changing texts.
Here is more detailed report of the error:
Unable to start activity ComponentInfo{com.inc.nicky.tapit/com.inc.nicky.tapit.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2452)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1302)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:1408)
at android.widget.TextView.setText(TextView.java:4949)
at com.inc.nicky.tapit.MainActivity.lookUpProduct(MainActivity.java:42)
at com.inc.nicky.tapit.MainActivity.onCreate(MainActivity.java:78)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
Here is MyDBHandler:
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "gameDB.db";
private static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public static final String COLUMN_QUANTITY = "quantity";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
TABLE_PRODUCTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_PRODUCTNAME
+ " TEXT," + COLUMN_QUANTITY + " INTEGER" + ")";
db.execSQL(CREATE_PRODUCTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
public void addProduct(Product product) {
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.getProductName());
values.put(COLUMN_QUANTITY, product.getQuantity());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
public Product findProduct(String productname) {
String query = "Select * FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " = \"" + productname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Product product = new Product();
if (cursor.moveToFirst()) {
cursor.moveToFirst();
product.setID(Integer.parseInt(cursor.getString(0)));
product.setProductName(cursor.getString(1));
product.setQuantity(Integer.parseInt(cursor.getString(2)));
cursor.close();
} else {
product = null;
}
db.close();
return product;
}
public boolean deleteProduct(String productname) {
boolean result = false;
String query = "DELETE FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + " = \"" + productname + "\"";
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(query);
db.close();
return result;
}
}
And the product Class:
public class Product {
private int _id;
private String _productname;
private int _quantity;
public Product() {}
public Product(int id, String productname, int quantity) {
this._id = id;
this._productname = productname;
this._quantity = quantity;
}
public Product(String productname, int quantity) {
this._productname = productname;
this._quantity = quantity;
}
public void setID(int id) {
this._id = id;
}
public int getID() {
return this._id;
}
public void setProductName(String productname) {
this._productname = productname;
}
public String getProductName() {
return this._productname;
}
public void setQuantity(int quantity) {
this._quantity = quantity;
}
public int getQuantity() {
return this._quantity;
}
}
What was I doing BEFORE the bug?
Upvotes: 0
Views: 121
Reputation: 1006839
setText(int)
expects the int
to be an ID of a string resource. getQuantity()
is not returning the ID of a string resource. Hence, setText(int)
will crash with a ResourceNotFoundException
.
Replace:
eee.setText(product.getQuantity());
with:
eee.setText(Integer.toString(product.getQuantity()));
This will call setText(String)
with the string representation of the int
returned by getQuantity()
.
Upvotes: 1