开始使用SQLite数据库后,Android App立即崩溃

时间:2017-07-29 18:11:01

标签: android database sqlite crash android-permissions

我创建了一个App来测试如何在Android App中实现SQLite数据库。但该应用程序甚至没有开始,我不知道是什么原因造成了麻烦。 这是我的代码:

logcat:

public class MainActivity extends AppCompatActivity {

    EditText buckysInput;
    TextView buckysText;
    MyDBHandbler dbHandbler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buckysInput = (EditText) findViewById(R.id.buckysInput);
        buckysText = (TextView) findViewById(R.id.buckysText);
        dbHandbler = new MyDBHandbler(this, null, null, 1);
        printDatabase();
    }

    public void addButtonClicked(View view){
        Products products = new Products(buckysInput.getText().toString());
        dbHandbler.addProduct(products);
        printDatabase();

    }
    public  void deleteButtonClicked(View view){
        String input = buckysInput.getText().toString();
        dbHandbler.deletProduct(input);
        printDatabase();
    }



    public void printDatabase() {
        String dbString = dbHandbler.databaseToString();
        buckysText.setText(dbString);
        buckysInput.setText("");
    }
}

我的SQLiteOpenHelperClass:

public class MyDBHandbler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "products.db";
    public static final String TABLE_PRODUCTS="products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";

    public MyDBHandbler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT" +
                COLUMN_PRODUCTNAME + "TEXT" +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_PRODUCTS);
        onCreate(db);
    }

    //Add a new row to the database
    public void addProduct(Products products){
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME, products.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();

    }

    //Delet Product from the database
    public void deletProduct(String productName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELET FROM" + TABLE_PRODUCTS + "WHERE" + COLUMN_PRODUCTNAME  +"=\"" + productName + "\";");
    }

    //Print out the databse as a String
    public String databaseToString(){
        String dbString ="";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM" + TABLE_PRODUCTS +"WHERE 1";

        //Cursor point to a location in your results
        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("productname"))!= null){
                dbString += c.getString(c.getColumnIndex("productname"));
                dbString += "\n";
            }
        }
        db.close();
        return dbString;
    }

}

产品类:

package com.example.phili.sqlitesmaple;

/**
* Created by phili on 29.07.2017.
*/

public class Products {

    private int _id;
    private String _productname;

    public  Products(){
    }

    public Products(String _productname){
        this._productname = _productname;
    }

    public void set_id(int _id){
        this._id = _id;
    }
    public void set_productname(String _productname){
        this._productname = _productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }
}

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phili.sqlitesmaple">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Logcat

:23838-23838/? I/art: Late-enabling -Xcheck:jni
    23838-23852/com.example.phili.sqlitesmaple E/HAL: load: id=gralloc != hmi->id=gralloc
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status

                                                      [ 07-29 20:26:08.842  3724: 3724 E/         ]
                                                      process stopped due to unexpected signal 13
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,div,atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a15 --instruction-set-features=default --dex-file=/data/app/com.example.phili.sqlitesmaple-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.example.phili.sqlitesmaple-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
    23838-23838/com.example.phili.sqlitesmaple W/System: ClassLoader referenced unknown path: /data/app/com.example.phili.sqlitesmaple-1/lib/arm
    23838-23838/com.example.phili.sqlitesmaple I/InstantRun: starting instant run server: is main process
    23838-23838/com.example.phili.sqlitesmaple I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
    23838-23838/com.example.phili.sqlitesmaple W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    23838-23838/com.example.phili.sqlitesmaple E/SQLiteLog: (1) 
    23838-23838/com.example.phili.sqlitesmaple I/Process: Sending signal. PID: 23838 SIG: 9

我认为它一定是SQLite数据库的问题,因为没有它可以正常工作。  我希望你能帮助我。来自德国的问候。

2 个答案:

答案 0 :(得分:0)

明确修复的一件事是onCreate SQL语句中的空格。使用字符串连接时,SQL语句无效:

String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
        COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT" +
        COLUMN_PRODUCTNAME + "TEXT" +
        ");";

将决定......

CREATE TABLE products(_idINTEGER PRIMARY KEY AUTOINCREMENTproductnameTEXT);

在所有SQL语句中也是如此,包括drop和select。

答案 1 :(得分:0)

不要使用printDatabase();因为在第一次你的数据库没有值,它将很可能返回null,所以不要在 OnCreate

上使用此方法

首先你的插入查询是错误的没有空格和逗号

您的查询

String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT" +
                COLUMN_PRODUCTNAME + "TEXT" +
                ");";

结果没有空格,也缺少逗号

CREATE TABLE products(_idINTEGER PRIMARY KEY AUTOINCREMENTproductnameTEXT);

修改后的查询

 String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT"+", " +
                COLUMN_PRODUCTNAME + " TEXT" +
                ");";

结果

CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT, productname TEXT);

和你的databaseToString方法中的第二个你的查询错误使用

    // type e.g id , productname
    String query = "SELECT * FROM " + TABLE_PRODUCTS +" WHERE type = 1";