我的启动画面不断崩溃,并强制关闭应用程序

时间:2018-09-07 16:09:42

标签: java android sqlite android-sqlite splash-screen

我的应用在启动时不断崩溃。请检查我的代码。我在初始屏幕上进行了自动登录。请帮助我,这是为了我的论文。

splash.class

import android.content.Intent;
import android.database.Cursor;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import static com.example.mgimoro.dietgo.Database.TABLE_NAME;

public class Splash extends AppCompatActivity {
SQLiteDatabase db;
SQLiteOpenHelper helper;
Database database;
int id=0;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    initialize();
}
public void initialize(){
    db=helper.getReadableDatabase();
    String query = "SELECT ID FROM " + TABLE_NAME
            + " WHERE ID = (ID)";
    Cursor c = db.rawQuery(query, null);
    if (c.moveToFirst()) {
        id=c.getInt(c.getColumnIndex(query));
    }else{}
    if (id<0){
        Intent homeIntent = new Intent(Splash.this, Onboard.class);
        startActivity(homeIntent);
        finish();
    }else{
        Intent homeIntent = new Intent(Splash.this, mainmenu.class);
        startActivity(homeIntent);
        finish();
      }
   }
  }

1 个答案:

答案 0 :(得分:1)

主要问题

当您在db=helper.getReadableDatabase();方法中尝试initialize时,您没有实例化 helper 对象,因此该对象为null。

因此,您需要在db=helper.getReadableDatabase();之前实例化辅助对象。

但是,根据SQLiteOpenHelper helper;. It should be a type of a subclass of SQLiteOpenHelper, as such you should instead use something like, mySqliteOpenHelperSubclass = myDBHelper`,您将无法实例化 helper ,因为它是使用SQLiteOpenHelper类型定义的。

然后在initialize方法中通过构造对象来实例化myDBhelper对象。例如myDBHelper = new mySqliteOpenHelperSubclass(this);,然后您可以使用myDBHelper.getWritableDatabase();

当前,您的代码将导致空指针异常。

补充问题

id=c.getInt(c.getColumnIndex(query));会导致异常(如果表中存在任何行),因为正在通知c.getColumnIndex()查找名为的列的列索引。选择ID FROM your_table_name WHERE ID = (ID),该名称将不存在。

结果将为-1,这是无效的索引,并且异常将指示无效的偏移量-1。

例如

09-07 21:16:24.903 1546-1546/? E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{so52226238.so52226238/so52226238.so52226238.Splash}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

工作示例

1 Database.java

以下内容假设 Database.java (假设它是SQLiteOpenHelper的子类)为:-

public class Database extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "mydb";
    public static final int DATABASE_VERSION = 1;
    public static final String TABLE_NAME = "mytable";
    public static final String COLUMN_ID = "ID";
    public static final String COLUMN_NAME = "name";

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

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String crtsql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY," +
                COLUMN_NAME + " TEXT" +
                ")";
        sqLiteDatabase.execSQL(crtsql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public long insert(String name) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_NAME,name);
        return db.insert(TABLE_NAME,null,cv);
    }
}
  • 显然您的版本可能更复杂

2 Splash.java

以下内容将解决问题(请参阅评论):-

public class Splash extends AppCompatActivity {
    SQLiteDatabase db;
    // SQLiteOpenHelper helper; <<<<<<<<<< DELETED
    Database database;
    int id=0;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        initialize();
    }
    public void initialize(){
        database = new Database(this); //<<<<< instantiate Database helper
        db=database.getReadableDatabase();
        database.insert("Fred"); //<<<<<<<<<< Added to test with data.
        String query = "SELECT ID FROM " + TABLE_NAME
                + " WHERE ID = (ID)";
        Cursor c = db.rawQuery(query, null);
        if (c.moveToFirst()) {
            id=c.getInt(c.getColumnIndex(COLUMN_ID)); //<<<<<<<<<< ID column
            //id=c.getInt(c.getColumnIndex("ID")); //<<<<<<<<<< as above but uses hard coded column name
        }else{}
        c.close(); //<<<<<<<<<< YOU SHOULD ALWAYS CLOSE A CURSOR WHEN FINISHED WITH IT
        if (id<0){
            Log.d("IDINFO","ID not found."); //<<<<<<<<<< used for simplicity
            /*
            Intent homeIntent = new Intent(Splash.this, Onboard.class);
            startActivity(homeIntent);
            finish();
            */
        }else{
            Log.d("IDINFO","ID found."); //<<<<<<<<<< used for simplicity
            /*

            Intent homeIntent = new Intent(Splash.this, mainmenu.class);
            startActivity(homeIntent);
            finish();
            */
        }
    }
}
  • 注意为方便起见和演示,已注释了开始活动的尝试。