简体   繁体   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

I am working on an android app. 我正在开发一个android应用程序。 I am trying to create an sql database which stores all the info stored by a user. 我正在尝试创建一个存储用户存储的所有信息的sql数据库。 Currently i'm trying to just work on the onCreate method of Database handler. 目前,我正在尝试仅使用数据库处理程序的onCreate方法。 i want to create the database, and store the first entry into the database through the onCreate method itself. 我想创建数据库,并通过onCreate方法本身将第一个条目存储到数据库中。 And read the entries and display it out in the HomeActivity using a TextView. 并读取条目,并使用TextView在HomeActivity中将其显示出来。

The following is the code for the home activity of my application. 以下是我的应用程序的家庭活动的代码。 I access a database handler class through this activity ( DBHandler ). 我通过此活动(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);
    }
}

This is the DBHandler Class. 这是DBHandler类。 PLEASE IGNORE THE COMMENTED TEXT AS I HAVE COMMENTED OUT THE ACTUAL DATA AND JUST WORKING WITH TEST DATA TO GET MY CONCEPTS STRAIGHT 请忽略注释文本,因为我注释了实际数据并仅与测试数据一起使用以使我的概念清晰明了

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

I think changing this lines as below should solve your problem:- 我认为如下更改此行应该可以解决您的问题:-

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

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

        setContentView(R.layout.activity_home);

Now i dont know why this worked, but what i did was creating a class which stored all the data before adding it the sql table. 现在我不知道为什么这样做,但是我所做的是创建一个存储所有数据的类,然后将其添加到sql表中。 I would like to research on why directly giving the values didn't work. 我想研究为什么直接给出这些价值观行不通。 I created the following class: 我创建了以下类:

public class CurrentLog { 公共类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.

相关问题 安卓 我正在尝试使用大型XML文件创建Train应用 - Android. I'm trying to create an Train app using large XML files 如何检查活动是从通知启动还是从用户单击 android 中的主屏幕仪表板应用程序图标启动 - how to check whether the activity was launched from notification or from the user click on home screen dashboard app icon in android 安卓。 SQLite 数据库 - Android. SQLite Database 我正在尝试在Android的正常活动中实现Listview。 我设法向Listview显示项目,但无法检测到点击 - I'm trying to implement Listview in an normal activity in Android. I managed to show items to Listview but I'm not being able to detect clicks 当我尝试从 SQLite 数据库中删除一行时,为什么我的 android 应用程序崩溃? Kotlin - Why is my android app crashing when I'm trying to delete a row from SQLite database? Kotlin Android 主屏幕小部件,从活动创建 - Android home screen widget, create from activity 每当我打开 RecyclerView 活动时,应用程序都会停止 - App keeps stopping whenever I open RecyclerView activity 安卓 使屏幕变黑 - Android. Making the screen go black 如何解决“启动屏幕冻结Android应用程序”活动? - How to fix 'Android App freezing at Splash Screen' activity? Android:App会继续预填充sqlite数据库 - Android: App keeps prepopulating the sqlite database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM