簡體   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