繁体   English   中英

在Android中创建SQlite数据库时应用程序冻结

[英]App freeezes while creating SQlite database in android

我试图按一下按钮将两个字符串值保存到数据库中。 当我按下按钮时,我的应用程序冻结。 我使用ADB Shell来查看是否正在创建.db文件,并且它显示一条消息“找不到文件”

调试应用程序时显示以下日志:

I/System.out: waiting for debugger to settle...

I/System.out: waiting for debugger to settle...

I/System.out: debugger has settled (1378)

W/System: ClassLoader referenced unknown path: /data/app

/com.thebitshoes.thereaders-2/lib/arm

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

D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb


I/OpenGLRenderer: Initialized EGL, version 1.4

W/art: Suspending all threads took: 16.983ms

W/art: Suspending all threads took: 10.735ms

W/art: Suspending all threads took: 8.821ms

W/art: Suspending all threads took: 6.433ms

W/art: Suspending all threads took: 7.269ms

W/art: Suspending all threads took: 6.203ms
I/art: Background partial concurrent mark sweep GC freed 83(2960B) AllocSpace objects, 60(15MB) LOS objects, 39% free, 24MB/40MB, paused 6.654ms total 45.830ms


W/art: Suspending all threads took: 5.955ms

W/art: Suspending all threads took: 7.533ms

I/art: Background partial concurrent mark sweep GC freed 77(2752B) AllocSpace objects, 56(15MB) LOS objects, 40% free, 19MB/32MB, paused 8.008ms total 34ms

W/art: Suspending all threads took: 9.923ms

W/art: Suspending all threads took: 5.833ms

这是我的DBHandler java文件:

public class DBHandler extends SQLiteOpenHelper {

//Basic settings for database
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="thereaders.db";

//Name of the table in database
public static final String TABLE_NAME="newgoalentry";

//Name of the columns of the table
public static String COLUMN_ID="id";
public static String COLUMN_TITLE="goalTitle";
public static String COLUMN_DESCRIPTION="goalDescription";


public DBHandler(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_NAME + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_TITLE + " TEXT," +
            COLUMN_DESCRIPTION + " TEXT " +
            ");";
    db.execSQL(query);
}

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

//this method will be called when CREATE button will be pressed for adding a new goal

public void addNewGoal(NewGoalEntry entry)
{
    try {
        ContentValues values = new ContentValues();
        values.put(COLUMN_TITLE, entry.getGoalTitle());
        values.put(COLUMN_DESCRIPTION, entry.getGoalDescription());

        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_NAME, null, values);
        db.close();
    }catch (Exception e)
    {
        e.printStackTrace();
    }


}

public String databaseToString()
{
    String dbString="";
    SQLiteDatabase db=getWritableDatabase();
    String query=" SELECT * FROM " +
            TABLE_NAME +
            " WHERE 1 ";

    Cursor c=db.rawQuery(query,null);
    c.moveToFirst();

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

}   db.close();
    return dbString;
}
}

这是活动Java类

public class CreateNoteActivity extends Activity {

EditText goal_title;
EditText goal_description;
TextView db_result;
Button create_goal;
DBHandler dbHandler;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.create_note2);

    db_result=(TextView)findViewById(R.id.database_view);
    goal_title=(EditText) findViewById(R.id.goal_title);
    goal_description=(EditText) findViewById(R.id.goal_description);
    create_goal=(Button)findViewById(R.id.create_goal);
    dbHandler=new DBHandler(this,null,null,1);
    printvalues();

}

public void printvalues()
{
    String dbString=dbHandler.databaseToString();
    db_result.setText(dbString);
    goal_title.setText("");
    goal_description.setText("");
}

public void addEntry(View view){
    Toast.makeText(getBaseContext(),"New Reading Goal created !!",Toast.LENGTH_SHORT).show();
    NewGoalEntry entry=new NewGoalEntry(goal_title.getText().toString(),goal_description.getText().toString());
    dbHandler.addNewGoal(entry);
    printvalues();


}


}

尝试删除db.close(); 每次读/写之后。 我想在我的一个应用程序上也实现一个简单的DB时,也会遇到类似的问题。 确保在您的应用不需要数据库时将其关闭,例如注销。

暂无
暂无

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

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