當我調用函數'測試'我的應用程序崩潰。任何想法爲什麼會發生?Android Studio應用程序崩潰,數據庫插入
DB類
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。示例。 paulcos ma.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:從應用程序獲取字體 資源失敗。 01-20 20:00 00:02.211 9565-9565/com.example.paulcosma.app2 E/SQLiteLog:(10)無法執行 文件讀取,得到: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:無法 Android的執行方法:的onClick
請不要發佈的代碼,XML,或logcat的輸出屏幕截圖。請以文字形式發佈所有文字。您還需要包含來自崩潰的堆棧跟蹤。 http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this http://stackoverflow.com/questions/3988788/what-is-a-stack-trace -and-如何燦我使用的,它到調試我的應用程序,錯誤 –