java(android)在模拟器上运行,但在实际设备上运行

时间:2014-09-29 18:01:37

标签: java android eclipse windows

这是一个Android的代码,这个代码将在模拟器上运行但不在实际设备上运行,基本上它不能在实际设备上创建一个表,而这可以在android模拟器上完成。

public class AddNewTransactionCategory extends ActionBarActivity {

    ListView listView;
    //ArrayList<String> categoryString;
    DataBaseTransactionAdapter dbAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_transaction_category);

        listView = (ListView) findViewById(R.id.categoryListView);
        dbAdapter = new DataBaseTransactionAdapter(this);
        dbAdapter.insertDataCategory("Shopping");

        // TODO Create a Table for Category
        Toast.makeText(this, dbAdapter.getAllDataCategory(), Toast.LENGTH_LONG).show();

        //ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, categoryString);
        //listView.setAdapter(adapter);
    }
}

这是用于创建表的DataBaseTransactionAdapter

public class DataBaseTransactionAdapter {

    DataBaseTransaction helper;

    public DataBaseTransactionAdapter(Context context) {
        helper = new DataBaseTransaction(context);
    }

    public long insertDataTransaction(int transaction) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DataBaseTransaction.TRANSACTION, transaction);
        // id will be negative if something went wrong otherwise it contains row id
        long id = db.insert(DataBaseTransaction.TABLE_NAME1, null, contentValues);
        return id;
    }

    public long insertDataCategory(String category) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(DataBaseTransaction.CATEGORY, category);
        // id will be negative if something went wrong otherwise it contains row id
        long id = db.insert(DataBaseTransaction.TABLE_NAME2, null, contentValues);
        return id;
    }

    public String getAllDataCategory() {
        // saving to ArrayList
        //ArrayList<String> arraylist = new ArrayList<String>();

        // showing data
        SQLiteDatabase db = helper.getWritableDatabase();
        String [] columns = {DataBaseTransaction.UID, DataBaseTransaction.CATEGORY};
        Cursor cursor = db.query(DataBaseTransaction.TABLE_NAME2, columns, null, null, null, null, null);
        StringBuffer buffer = new StringBuffer();
        while (cursor.moveToNext())
        {
            // behtare bejaye 0 az int index = cursor.getColumnIndex(DataBaseTransaction.UID)
            int cid = cursor.getInt(0);

            String category = cursor.getString(cursor.getColumnIndex(DataBaseTransaction.CATEGORY));
            //arraylist.add(category);

            buffer.append(cid + " " + category + "\n");
        }

        return buffer.toString();

        //return Integer.toString(5);
        //return arraylist;
    }


    static class DataBaseTransaction extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "hulkdb";
        private static final int DATABASE_VERSION = 1;

        private static final String TABLE_NAME1 = "transaction";
        private static final String TABLE_NAME2 = "category";

        private static final String UID = "_id";
        private static final String TRANSACTION = "usertransaction";
        private static final String CATEGORY = "category";

        private static final String CREATE_TABLE1 = "CREATE TABLE " + TABLE_NAME1
                + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TRANSACTION
                + " INTEGER);";
        private static final String CREATE_TABLE2 = "CREATE TABLE "+ TABLE_NAME2 +" ("+ UID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+CATEGORY+" VARCHAR(255));";
        private static final String DROP_TABLE = "DROP TABLE IF EXISTS "
                + TABLE_NAME1;

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

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {

                // CREATE TABLE transaction (_id INTEGER PRIMARY KEY AUTOINCREMENT, transaction INTEGER);
                db.execSQL(CREATE_TABLE1);

                // CREATE TABLE category (_id INTEGER PRIMARY KEY AUTOINCREMENT, category VARCHAR(255));
                db.execSQL(CREATE_TABLE2);
            }
            catch (SQLException e) {
                Log.e("ERROR","SQL on create");
            }

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                db.execSQL(DROP_TABLE);
                onCreate(db);
            } catch (SQLException e) {
                Log.e("ERROR","SQL on upgrade");
            }
        }

    }
}

LogCat中的错误是:没有这样的表:category

问题在于这行代码: Cursor cursor = db.query(DataBaseTransaction.TABLE_NAME2,columns,null,null,null,null,null);

0 个答案:

没有答案