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

标签 android mysql sqlite gradle

我有一个程序,我使用 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/

相关文章:

android - 如何知道 HTML5 中的音频路径(Chrome 浏览器 - iOS/Android)

php - 为什么我的 PHP 搜索引擎找不到任何记录?

php - 从搜索中返回其他列数据

ruby-on-rails - ruby rails : Issue sorting index based on average review rating

c# - 我应该在 Xamarin.Forms 上使用哪些 SQLite (sqlite-net-pcl) 标志?

Android studio 3.5重构问题

android - 使用 pg 选项在 Android 上进行分析

android - 重新打开 ROOM 数据库

php - 使用 MySQL 禁止通配符 IP

sql - 如何选择一列中的列值重复但另一列中的列值不同的行?