当我调用'test'函数时,我的应用程序崩溃了。任何想法为什么会发生这种情况?
数据库类
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: 无法在APK中找到切片;中止。 01-20 19:59:47.988 9565-9565 / com.example.paulcosma.app2 E / dalvikvm:找不到课程 'android.graphics.drawable.RippleDrawable',从方法引用 android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 01-20 19:59:48.005 9565-9565 / com.example.paulcosma.app2 E / TextView: zhangcl:从应用程序获取字体资源失败。 01-20 19:59:48.013 9565-9565 / com.example.paulcosma.app2 E / TextView:zhangcl: 从应用程序获取字体资源失败 - 2. 01-20 19:59:48.045 9565-9565 / com.example.paulcosma.app2 E / TextView:zhangcl:get font 应用程序资源失败。 01-20 20:00:02.211 9565-9565 / com.example.paulcosma.app2 E / SQLiteLog:(10)无法做到 file read,got:0,amt:100,last Errno:2 01-20 20:00:02.244 9565-9565 / com.example.paulcosma.app2 E / SQLiteLog:(1)未知数据库 食品01-20 20:00:02.263 9565-9565 / com.example.paulcosma.app2 E / AndroidRuntime:致命例外:主要 过程:com.example.paulcosma.app2,PID:9565
java.lang.IllegalStateException:无法执行方法 机器人:的onClick
答案 0 :(得分:0)
你在onCreate()
中输了一个拼写错误。您正在使用dbName
而不是tableName
来创建表格,因此,当您运行查询 查找正确的名称时,您就可以&# 39;重新查找一张不存在的表格。所以:
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)");