Android Studio 应用程序在插入数据库时​​崩溃

Android Studio Application crash on database insertion

当我调用函数 'test' 时,我的应用程序崩溃了。知道为什么会这样吗?

DB class

package com.example.paulcosma.app2;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DB extends SQLiteOpenHelper{
    public static final String
            dbName = "foods.db",
            tableName = "foods",
            colId = "id",colName = "name",colCarbs = "carbs";

    public DB(Context context) {
        super(context, dbName, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + dbName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + dbName);
        onCreate(db);
    }

    public boolean addFood(String Name, float Carbs){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(colName,Name);
        contentValues.put(colCarbs,Carbs);
        long result = db.insert(tableName,null,contentValues);

        return result != -1; // -1 == not inserted
    }
}

MainActivity

package com.example.paulcosma.app2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    DB db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new DB(this);
    }
    public void test(View v){
        boolean insertedSuccessfully = db.addFood("Milk",4); // crashes when called
        if(insertedSuccessfully)
            Toast.makeText(this,"inserted",Toast.LENGTH_LONG).show();
        else Toast.makeText(this,"not inserted",Toast.LENGTH_LONG).show();
    }

}

logcat

01-20 19:59:47.888 9565-9565/com.example.paulcosma.app2 E/InstantRun: Could not find slices in APK; aborting. 01-20 19:59:47.988 9565-9565/com.example.paulcosma.app2 E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 01-20 19:59:48.005 9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl: get font resource from application failed. 01-20 19:59:48.013 9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl: get font resource from application failed -- 2. 01-20 19:59:48.045 9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl: get font resource from application failed. 01-20 20:00:02.211 9565-9565/com.example.paulcosma.app2 E/SQLiteLog: (10) Failed to do file read, got: 0, amt: 100, last Errno: 2 01-20 20:00:02.244 9565-9565/com.example.paulcosma.app2 E/SQLiteLog: (1) unknown database foods 01-20 20:00:02.263 9565-9565/com.example.paulcosma.app2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paulcosma.app2, PID: 9565
java.lang.IllegalStateException: Could not execute method for android:onClick

您在 onCreate() 中有错字。您正在使用 dbName 而不是 tableName 来创建您的 table,因此当您 运行 您的查询 时查找正确的名称,您正在查找不存在的 table。所以:

db.execSQL("create table " + dbName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");

需要成为:

db.execSQL("create table " + tableName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");