连接到SQLite并尝试访问其方法时,应用程序崩溃

时间:2018-10-06 13:16:38

标签: android sqlite

我正在尝试制作一个使用Firebase来存储学生的电子邮件和密码的应用程序。但是对于存储任何特定用户的登录会话(例如,如果该用户尚未显式注销并终止该应用程序,则不会要求他/她再次登录并将其直接带到HomeScreen)使用SQLite数据库。但是我的应用程序在尝试访问MyDatabasehelper类中的方法的主屏幕本身上崩溃了。

1)Mydatabasehelper.class     公共类MyDatabaseHelper扩展了SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1; //databse version always starts with 1 (NOT 0)
public static final String TABLE_NAME = "Login";
private static final String DATABASE_NAME = "login.db";
private static final String COLUMN_0_Id = "Id";
private static final String COLUMN_1_IsLogin = "LoginData";

//override these 2 methods (otherwise error on the inheritance)

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

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    //2 ways -> 1) create string variable and store the query in it 2)
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_NAME + "(Id INT PRIMARY KEY, IsLogin INT)";
    sqLiteDatabase.execSQL(CREATE_LOGIN_TABLE); //executing the query using the SQLiteDatabase object 'db' that we get as a parameter
    Insert();

}

//if we are changing the schema of our database, the database gets 'upgraded'
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(sqLiteDatabase);
}

private void Insert() {
    //class which allows us to specify values in the 'key - value' pair way
    ContentValues values = new ContentValues();

    values.put(COLUMN_0_Id, 1);
    values.put(COLUMN_1_IsLogin,0);

    SQLiteDatabase db = this.getWritableDatabase();
    long r = db.insert(TABLE_NAME, null, values);
    Log.d("Return Value : ", String.valueOf(r));
    db.close();
}

public LoginData Search(int StuRN) {
    SQLiteDatabase db = this.getWritableDatabase();

    String query = "SELECT * FROM " + TABLE_NAME + " WHERE Id=" + StuRN;
    Cursor result = db.rawQuery(query, null);

    LoginData logindata=new LoginData();

    if (result.moveToFirst()) {
        logindata.setId(StuRN);
        logindata.setIsLogin(result.getInt(1));

        result.close();
    }
    else {
        logindata = null;
    }
    return logindata;

}

public void updateData(int IsLogin) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_1_IsLogin, IsLogin);
    db.update(TABLE_NAME, contentValues, COLUMN_0_Id + " = " + "1", null);

}
    }

2)MainActivity.class         公共类MainActivity扩展了AppCompatActivity {

private static int SPLASH = 2000;
//private Cursor cursor;
MyDatabaseHelper dbHandler;
LoginData logindata;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbHandler = new MyDatabaseHelper(MainActivity.this, null, null, 1);
    logindata = new LoginData();

    LoginData l=dbHandler.Search(1);

    final int display=logindata.getIsLogin();
    new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this,display,Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, Login.class);
                startActivity(intent);
                finish();

            }
        },SPLASH);



}
    }

3)错误     AndroidRuntime:致命异常:main                   流程:com.example.android.cummins,PID:22698                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.android.cummins / com.example.android.cummins.MainActivity}:java.lang.IllegalStateException:getDatabase递归调用                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2560)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626)                       在android.app.ActivityThread.-wrap11(ActivityThread.java)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1475)                       在android.os.Handler.dispatchMessage(Handler.java:111)                       在android.os.Looper.loop(Looper.java:207)                       在android.app.ActivityThread.main(ActivityThread.java:5740)                       在java.lang.reflect.Method.invoke(本机方法)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)                    引起原因:java.lang.IllegalStateException:getDatabase递归调用                       在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:203)                       在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)                       在com.example.android.cummins.MyDatabaseHelper.Insert(MyDatabaseHelper.java:49)                       在com.example.android.cummins.MyDatabaseHelper.onCreate(MyDatabaseHelper.java:30)                       在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)                       在android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)                       在com.example.android.cummins.MyDatabaseHelper.Search(MyDatabaseHelper.java:56)                       在com.example.android.cummins.MainActivity.onCreate(MainActivity.java:26)                       在android.app.Activity.performCreate(Activity.java:6543)                       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2513)                       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626)                       在android.app.ActivityThread.-wrap11(ActivityThread.java)                       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1475)                       在android.os.Handler.dispatchMessage(Handler.java:111)                       在android.os.Looper.loop(Looper.java:207)                       在android.app.ActivityThread.main(ActivityThread.java:5740)                       在java.lang.reflect.Method.invoke(本机方法)                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766) I / Process:正在发送信号。 PID:22698 SIG:9 应用已终止。

0 个答案:

没有答案