ANDROID STUDIO:Gradle不构建sql数据库

时间:2015-07-27 11:40:07

标签: 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说它无法将Text更改为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. 我更改了应用程序的图标,调试/ AndroidManifest出现了,用红色文字说明:这里不允许这样做,这是不允许的。从那以后SQL就不起作用了,虽然它工作得很好

1 个答案:

答案 0 :(得分:1)

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

替换:

eee.setText(product.getQuantity());

使用:

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

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