应用程序崩溃" Getwritabledatabase()"

时间:2014-10-01 14:11:19

标签: java android sql database sqlite

我是Java新手,更不用说Android应用开发了。

我试图检索创建的表时,我的应用程序崩溃了。我非常确定代码是可靠的,并且我怀疑我的查询导致了崩溃。所以我有两个可能导致这种情况的查询,CREATE TABLE,然后是getWritableDatabase()......

我的主要活动是发号施令:

package com.bb.android.breakingbread;

import android.app.Activity;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class MenuActivity extends Activity {

    SQLiteDatabase db;
    TableRow tableRow;
    private static final int NUM_COL=3;
    private static final int NUM_ROW =3;
    Button MenuButtons[][]=new Button[NUM_ROW][NUM_COL];
    String [][] MenuTable = new String [NUM_ROW][NUM_COL];
    String[][] Checker;
    double [][] Bill = new double [20][40];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LayoutMenuSC();
        DBhelper db = new DBhelper(this);
        Checker=db.getMenu();

    }

    //============SET LAYOUT OF MENU PAGE (P2)=================================================
    public void LayoutMenuSC(){
        // layout the buttons on screen
        setContentView(R.layout.activitymenu);
        TableLayout table =(TableLayout) findViewById(R.id.MenuButtonTable); //need to link this table to xml

        for (int row=0; row<NUM_ROW;row++){
            //Set new Row in Table
            TableRow tableRow=  new TableRow(this);
            tableRow.setLayoutParams(new TableLayout.LayoutParams(
                  TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT,1.0F));
            table.addView(tableRow);

            for (int col=0; col<NUM_COL; col++){
                int x,y;
                float w;
                ImageButton ImButton=new ImageButton(this);
                ImButton.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1.0F));

                Resources res = getResources();
                Drawable drawable = res.getDrawable(R.drawable.ic_launcher);
                //Drawable drawable= res.getDrawable("R.drawable." + Photopath);<--------------------
                ImButton.setBackground(drawable);
                tableRow.addView(ImButton);
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.breaking_bread, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我的DBhelper课程是这样的:

package com.bb.android.breakingbread;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import java.util.LinkedList;
import java.util.List;


public class DBhelper extends SQLiteOpenHelper {

    private static final String TAG= DBhelper.class.getSimpleName();
    public static final String DB_NAME="BreakingBreadDB6";
    public static final int DB_VERSION=1;

    private static final String TABLE_MENU = "MenuTable";
    private static final String KEY_ID = "id";
    private static final String KEY_SW = "SW";
    private static final String KEY_PRICE = "Price";
    private static final String KEY_PHOTOPATH = "Photopath";

    // constructor
    public DBhelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // SQL statement to create menu table
        String CREATE_MENU_TABLE = "CREATE TABLE IF NOT EXISTS "+ TABLE_MENU +" ( " + KEY_ID + "     INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_SW + " TEXT, "+
            KEY_PRICE +" TEXT, " +
            KEY_PHOTOPATH+ " TEXT )";

        // create  table
        db.execSQL(CREATE_MENU_TABLE);

        // populate table
        AddSWRow("BBQ","15","Path1");
        //AddSWRow("Spicy Turky",12,"Path2");
        //AddSWRow("Chicken Melt",20,"Path3");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older items table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MENU);
        // create fresh menu table
        this.onCreate(db);
    }

    //====================ADD SANDWICH TO TABLE===================
    public void AddSWRow (String SwName, String SwPrice, String SwPhoto){

        // 1. get reference to writable DB
        SQLiteDatabase db =this.getWritableDatabase();

        // 2. create ContentValues to add key "column"/value
        ContentValues values = new ContentValues();
        values.put(KEY_SW,SwName);
        values.put(KEY_PRICE, SwPrice);
        values.put(KEY_PHOTOPATH, SwPhoto);

        // 3. insert
        db.insert(TABLE_MENU,null,values); // key/value -> keys = column names/ values = column      values

        // 4. close*/
        db.close();
    }

    //===================GET MENU================================
    public String[][] getMenu() {
        // List<Menu> Menu = new LinkedList<Menu>();
        String MenuArray[][] = new String[50][4];
        //0. initiatlise array with EOF..
        for (int i=0; i<50;i++){
            for (int j=0; j<4;j++){
                MenuArray[i][j]="EOF";
            }
        }

        // 1. build the query
        String query = "SELECT  * FROM " + TABLE_MENU;

        // 2. get reference to writable DB
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(query, null);

        // 3. go over each row

        Menu menu = null;
        int i;
        i=0;

        if (cursor != null){
            cursor.moveToFirst();
            /*while (cursor.isAfterLast() == false) {
                MenuArray[i][0]=Integer.parseInt(cursor.getString(0));
                MenuArray[i][1]=cursor.getString(1);
                MenuArray[i][2]=cursor.getString(2);
                MenuArray[i][3]=cursor.getString(3);

                i++;
                cursor.moveToNext();
            }*/
            cursor.close();
        }
        return MenuArray;
    }
}

任何建议?

1 个答案:

答案 0 :(得分:2)

您无法递归调用getWritableDatabase()

您的addSWRow()来电getWritableDatabase(),您在数据库帮助addSWRow()中呼叫onCreate()onCreate()触发getWritableDatabase()

移除addSWRow()来电,或将SQLiteDatabaseonCreate()传递给它,而不是在那里拨打getWritableDatabase()