mardi 2 juin 2015

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. I am trying to create an sql database which stores all the info stored by a user. Currently i'm trying to just work on the onCreate method of Database handler. i want to create the database, and store the first entry into the database through the onCreate method itself. And read the entries and display it out in the HomeActivity using a TextView.

The following is the code for the home activity of my application. I access a database handler class through this activity ( 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. 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;
    }
}

Aucun commentaire:

Enregistrer un commentaire