繁体   English   中英

我正在尝试在android中创建SQLite数据库。 但是,每当启动家庭活动时,该应用都会保持黑屏冻结

[英]I'm trying to create SQLite database in android. But the app keeps freezing with a black screen whenever home activity is launched

我正在开发一个android应用程序。 我正在尝试创建一个存储用户存储的所有信息的sql数据库。 目前,我正在尝试仅使用数据库处理程序的onCreate方法。 我想创建数据库,并通过onCreate方法本身将第一个条目存储到数据库中。 并读取条目,并使用TextView在HomeActivity中将其显示出来。

以下是我的应用程序的家庭活动的代码。 我通过此活动(DBHandler)访问数据库处理程序类。

public class HomeActivity extends Activity {


    Button addCashButton,addExpenditureButton;
    private DBHandler dbHandler;
    TextView logText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        dbHandler = new DBHandler(this,null,null,1);
        logText = (TextView)findViewById(R.id.logText);

        addCashButton = (Button)findViewById(R.id.addCashButton);
        addExpenditureButton = (Button) findViewById(R.id.addExpenditureButton);

        //Set Log
        logText.setText(dbHandler.showRecentLog(this));
    }



    //ADD CASH BUTTON CLICKED - Launch add cash activity
    public void launchAddCash(View view) {
        Intent addcash = new Intent(HomeActivity.this,AddCashActivity.class);
        startActivity(addcash);
    }
}

这是DBHandler类。 请忽略注释文本,因为我注释了实际数据并仅与测试数据一起使用以使我的概念清晰明了

public class DBHandler extends SQLiteOpenHelper {

    //Declaring Variables
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "test4db.db";
    //Table Name
    public static final String TABLE_ACCOUNT = "test1";
    //Column Names
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_TITLE = "_title";
    public static final String COLUMN_DESCRIPTION = "_description";
    public static final String COLUMN_DATE = "_date";
    public static final String COLUMN_TIME = "_time";
    public static final String COLUMN_CASH1 = "_cash1";
    public static final String COLUMN_CASH2 = "_cash2";
    public static final String COLUMN_CASH3 = "_cash3";
    public static final String COLUMN_CASH4 = "_cash4";
    public static final String COLUMN_CASH5 = "_cash5";
    public static final String COLUMN_CASH6 = "_cash6";
    public static final String COLUMN_CASH7 = "_cash7";
    public static final String COLUMN_CASH8 = "_cash8";
    public static final String COLUMN_CASH9 = "_cash9";
    public static final String COLUMN_CASH10 = "_cash10";
    public static final String COLUMN_AMOUNT = "_amount";
    Context context;
//    SQLiteDatabase db = getWritableDatabase();


    //Constructor
    public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
        this.context=context;
    }


    //ON CREATE METHOD
    @Override
    public void onCreate(SQLiteDatabase db) {


        //Test Query
        String query = "CREATE TABLE " + TABLE_ACCOUNT + " (" + COLUMN_TITLE + " TEXT " + ");";

        db.execSQL(query);

        defaultLog(db);
        Toast.makeText(context,"onCreate",Toast.LENGTH_SHORT).show();

    }



    //DEFAULT LOG METHOD
    public void defaultLog(SQLiteDatabase db){


        //Test default
        String query = "INSERT INTO " + TABLE_ACCOUNT + " (" + COLUMN_TITLE + ") VALUES (" + "\"TITLE\"" + ");";
        db.execSQL(query);
        Toast.makeText(context,"defaultLog",Toast.LENGTH_SHORT).show();
    }

    //ON UPGRADE METHOD
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ACCOUNT);
        onCreate(db);
        Toast.makeText(context,"onUpgrade",Toast.LENGTH_SHORT).show();
    }


    //Adding new log to the database
    public void addAmountLog(String title,String description,int amount,int np1,int np2, int np3, int np4, int np5, int np6, int np7, int np8, int np9, int np10,Context context){

        String query = "INSERT INTO " + TABLE_ACCOUNT + " (" + COLUMN_TITLE + ", " + COLUMN_DESCRIPTION + ", "
                + COLUMN_CASH1 + ", "
                + COLUMN_CASH2 + ", "  + COLUMN_CASH3 + ", "  + COLUMN_CASH4 + ", "  + COLUMN_CASH5 + ", "
                + COLUMN_CASH6 + ", "  + COLUMN_CASH7 + ", "  + COLUMN_CASH8 + ", "  + COLUMN_CASH9 + ", "
                + COLUMN_CASH10 + ", "
                + COLUMN_AMOUNT + ") VALUES ("
                + title + ", " + description + ", " + amount + ", "
                + np1 + ", " + np2 + ", " + np3 + ", " + np4 + ", " + np5 + ", " + np6 + ", "
                + np7 + ", " + np8 + ", " + np9 + ", " + np10 + ");";

        Toast.makeText(context,title + " " + description,Toast.LENGTH_SHORT).show();
    }


    //Printing the log
    public String showRecentLog(Context context){
        String logString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_ACCOUNT + " WHERE 1;";

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

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("_title"))!=null){
                logString += c.getString(c.getColumnIndex("_title"));
                logString += "\n";
            }
        }
        c.close();
        db.close();
        Toast.makeText(context,logString + "showRecentLog",Toast.LENGTH_SHORT).show();
        return logString;
    }
}

我认为如下更改此行应该可以解决您的问题:-

super.onCreate(savedInstanceState); //this should be 1st line

 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_home);

现在我不知道为什么这样做,但是我所做的是创建一个存储所有数据的类,然后将其添加到sql表中。 我想研究为什么直接给出这些价值观行不通。 我创建了以下类:

公共类CurrentLog {

private int _id;
private String _title,_description;
private int _amount,_np1,_np2,_np3,_np4,_np5,_np6,_np7,_np8,_np9,_np10;
private long _balance;

public CurrentLog (){

}

public void set_id(int _id) {
    this._id = _id;
}

public void set_title(String _title) {
    this._title = _title;
}

public void set_description(String _description) {
    this._description = _description;
}

public void set_amount(int _amount, int balance) {
    this._amount = _amount;
    set_balance(_amount + balance);
}

public void set_np1(int _np1) {
    this._np1 = _np1;
}

public void set_np2(int _np2) {
    this._np2 = _np2;
}

public void set_np3(int _np3) {
    this._np3 = _np3;
}

public void set_np4(int _np4) {
    this._np4 = _np4;
}

public void set_np5(int _np5) {
    this._np5 = _np5;
}

public void set_np6(int _np6) {
    this._np6 = _np6;
}

public void set_np7(int _np7) {
    this._np7 = _np7;
}

public void set_np8(int _np8) {
    this._np8 = _np8;
}

public void set_np9(int _np9) {
    this._np9 = _np9;
}

public void set_np10(int _np10) {
    this._np10 = _np10;
}

public void set_balance(long _balance) {
    this._balance = _balance;
}

public long get_balance() {
    return _balance;
}

public int get_id() {
    return _id;
}

public String get_title() {
    return _title;
}

public String get_description() {
    return _description;
}

public int get_amount() {
    return _amount;
}

public int get_np1() {
    return _np1;
}

public int get_np2() {
    return _np2;
}

public int get_np3() {
    return _np3;
}

public int get_np4() {
    return _np4;
}

public int get_np5() {
    return _np5;
}

public int get_np6() {
    return _np6;
}

public int get_np7() {
    return _np7;
}

public int get_np8() {
    return _np8;
}

public int get_np9() {
    return _np9;
}

public int get_np10() {
    return _np10;
}

}

暂无
暂无

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

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