简体   繁体   中英

App freeezes while creating SQlite database in android

I am trying to save two string values into a database on the press of a button. When i press the button then my app freezes . I used ADB shell to see if the .db file is being created or not and it shows a message of "file not found

The following log is shown while debugging the app:

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

Here's my DBHandler java file :

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;
}
}

Here's the activity java class

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();


}


}

Try removing db.close(); after each read/write. I think I faced a similar issue when I implemented a simple DB on one of my apps as well. Make sure to close the DB when your app won't need it, such as logout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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