简体   繁体   English

为什么我的应用程序在手机上崩溃,但在AVD模拟器上却没有崩溃?

[英]Why does my app crash in my mobile phone but not on AVD emulator?

I've written an app that uses SQLite db. 我编写了一个使用SQLite db的应用程序。 At the first stage when I wasn't using SQLite and just working with ordinary and usual fields to store data, my app was working just fine in my mobile phone. 在我不使用SQLite而是仅使用普通字段和普通字段存储数据的第一阶段,我的应用程序在手机中的运行情况就很好。 But Now when I'm using SQLite to efficiently store and extract data, my app crashes in my mobile phone. 但是现在,当我使用SQLite有效存储和提取数据时,我的应用程序在手机中崩溃了。 The interesting point is that it is working well on AVD emulator in my computer. 有趣的一点是,它在我的计算机上的AVD模拟器上运行良好。 Any idea why this is happening? 知道为什么会这样吗?

This is my DB class: 这是我的数据库类:

public class WordsDB extends SQLiteOpenHelper {

    static final String DATABASE_NAME = "DicDB";

    static final String TermTable = "Terms";
    static final String ID = "id";
    static final String Selected = "selected";
    static final String Term = "term";
    static final String Br = "br_phon";
    static final String NAm = "nam_phon";
    static final String Type = "type";
    static final String Noun = "noun";
    static final String Verb = "verb";
    static final String Adjective = "adjective";
    static final String Adverb = "adverb";

    static final String DefTable = "Definitions";
    static final String Def = "definition";
    static final String Ex = "example";

    public static final int DATABASE_VERSION = 1;

    private static final String TERM_TABLE_CREATE = "Create table " + TermTable +
        "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
        Selected + " VARCHAR(5) ," +
        Term + " VARCHAR(20) ," +
        Br + " VARCHAR(20) ," +
        NAm + " VARCHAR(20) ," +
        Type + " VARCHAR(10) ," +
        Noun + " VARCHAR(20) ," +
        Verb + " VARCHAR(20) ," +
        Adjective + " VARCHAR(20) ," +
        Adverb + " VARCHAR(20)) ";

    private static final String DEF_TABLE_CREATE = "Create table " + DefTable +
        "(" + ID + " INTEGER PRIMARY KEY ," +
        Def + " VARCHAR(30) ," +
        Ex + " VARCHAR(160)) ";

    public WordsDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
    db.execSQL(TERM_TABLE_CREATE);
    db.execSQL(DEF_TABLE_CREATE);
    }

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

And here is the DataSource class: 这是DataSource类:

public class WordsDS {

    private WordsDB dbHelper;
    private SQLiteDatabase db;

    public WordsDS(Context context) {
        dbHelper = new WordsDB(context);
    }

    public void open() throws SQLException {
        db = dbHelper.getWritableDatabase();
    }

    public void close() {
        db.close();
    }

    // Methods to inset, update and extract data from table
    .......
}

I create an object out of WordsDS class in my main activity and through open method I get writable database and so forth and in my other classes I use the WordsDS object and insert, update and extract data through it. 我在主要活动中从WordsDS类中创建一个对象,并通过开放方法获得可写数据库,依此类推;在其他类中,我使用WordsDS对象并通过该对象插入,更新和提取数据。

First you must uninstall the app to make a fresh install. 首先,您必须卸载应用程序才能进行全新安装。 Meaning all the database created will be wiped on your phone. 这意味着所有创建的数据库将在您的手机上擦除。 This will ensure that if a new column is added or changes in database structure occurs the database will be created. 这将确保如果添加了新列或发生了数据库结构更改,那么将创建数据库。 You might think that the onUpgrade method will do this trick for you but it will not! 您可能会认为onUpgrade方法可以为您完成此操作,但不会成功!

As for debugging your app on mobile. 至于在移动设备上调试应用程序。 First you must install the drivers of your phone on your computer. 首先,您必须在计算机上安装手机的驱动程序。 Then you can now run the adb logcat afterwards. 然后,您现在可以随后运行adb logcat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM