gpt4 book ai didi

安卓工作室 : Gradle does not build sql database

转载 作者:行者123 更新时间:2023-11-29 12:05:02 24 4
gpt4 key购买 nike

我有一个程序,我使用 SQL 来存储用户信用。它曾经运行良好,直到有一天它停止运行。这是问题首先出现的地方。

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 表示它无法将文本更改为product.getQuantity()。然而,如果我删除这一行并开始使用 SQL,问题仍然存在;即使我没有更改文本,我也无法调用product.getQuantity()。

这是更详细的错误报告:

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)

           

这是 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;
}
}

产品类别:

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;
}

}

在出现错误之前我在做什么?

  1. 我导入了 Facebook SDK 并运行了该应用程序几次 - 效果很好
  2. 我更改了应用程序的图标,出现了 debug/AndroidManifest,并用红色文字覆盖:这里不允许,那里不允许。从那时起,尽管 SQL 工作得很好,但它就不再工作了

最佳答案

setText(int) 期望 int 是字符串资源的 ID。 getQuantity() 未返回字符串资源的 ID。因此,setText(int) 将崩溃并出现 ResourceNotFoundException

替换:

eee.setText(product.getQuantity());

与:

eee.setText(Integer.toString(product.getQuantity()));

这将使用 getQuantity() 返回的 int 的字符串表示形式调用 setText(String)

关于安卓工作室 : Gradle does not build sql database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31652115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com