不受支持的操作异常从SQLite android获取数据

时间:2015-03-17 21:50:41

标签: android sqlite

我已经按照youtube上的幻灯片书呆子的教程从android上的数据库中获取数据,但我无法使其工作。我已经尝试了一整天,但我真的不明白。

以下是这两个类的代码(我已经记录了一点,因为我想找到问题的根源):

public class MainBoard extends ActionBarActivity {
    public String TAG = "ADA";
//  private RecyclerView recyclerView;
    private String key;
    private List<Data> dataList = Collections.emptyList();
//    private RecyclerAdapter recyclerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG,"Start onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainboard);
        key = getIntent().getStringExtra("key");
        Log.i(TAG,"recycler is about to get called");
//        recyclerView = (RecyclerView) findViewById(R.id.passList);
        Log.i(TAG,"recycler  called");
        getData();
        Log.i(TAG,"getdata called");


//        recyclerAdapter = new RecyclerAdapter(this, dataList);
//        recyclerView.setAdapter(recyclerAdapter);
//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }


    public void getData(){
        Log.i(TAG,"starting getData()");
        DatabaseAdapter databaseAdapter = new DatabaseAdapter(this);
        Log.i(TAG,"adapter created");
        if(databaseAdapter.getAllData() != null) {
            Log.i(TAG,"datalist not null");
            try {
                this.dataList = databaseAdapter.getAllData();
            }catch(Exception e){
                Log.i(TAG,e.getMessage());
            }
        }
        else{
            Log.i(TAG,"datalist null");
        }
    }
}


    public class DatabaseAdapter {

    DatabaseHelper helper ;
    Context context;


    public DatabaseAdapter(Context context){
        helper = new DatabaseHelper(context);
        this.context=context;
    }

    public long insertData(String title, String login, String password){

        ContentValues contentValues = new ContentValues();
        contentValues.put(helper.COL_TITLE,title);
        contentValues.put(helper.COL_LOGIN,login);
        contentValues.put(helper.COL_PASSWORD,password);
        SQLiteDatabase db = helper.getWritableDatabase();

        long result = db.insert(helper.TABLE_NAME,null,contentValues);
        db.close();
        return result;

    }
    public List<Data> getAllData(){
        List<Data> dataList = Collections.emptyList();
        SQLiteDatabase db = helper.getWritableDatabase();
        String[] columns = {helper.UID,helper.COL_TITLE,helper.COL_LOGIN,helper.COL_PASSWORD};
        Cursor cursor = db.query(helper.TABLE_NAME,columns,null,null,null,null,null);
        while(cursor.moveToNext()){
            Data newData = new Data();
            newData.setId(cursor.getInt(0));
            newData.setTitle(cursor.getString(1));
            newData.setLogin(cursor.getString(2));
            //newData.setPasswortd(cursor.getString(3));
            dataList.add(newData);
        }
        cursor.close();
        db.close();
        return dataList;
    }

    static class DatabaseHelper extends SQLiteOpenHelper                                 {
        private static final String DATABASE_NAME = "PassDB";
        private static final String TABLE_NAME = "Passwords";
        private static final String UID = "_id";
        private static final String COL_TITLE = "title";
        private static final String COL_LOGIN = "login";
        private static final String COL_PASSWORD = "password";
        private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " ("
                + UID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COL_TITLE + " VARCHAR(255)," + COL_LOGIN + " VARCHAR(255),"
                + COL_PASSWORD + " VARCHAR(255));";

        private static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NAME + ";";
        private static final int DATABASE_VERSION = 1;
        Context c;


        public DatabaseHelper(Context context) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            c=context;
            Toast.makeText(c, "constructor called", Toast.LENGTH_SHORT).show();
            Log.i("ADA", CREATE_TABLE);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(CREATE_TABLE);
                Toast.makeText(c, "onCreate succesfull", Toast.LENGTH_SHORT).show();

            } catch (Exception e) {
                Log.i("SQL",e.getMessage());
                Toast.makeText(c, "onCreate not succesfull"+e, Toast.LENGTH_SHORT).show();
            }
            db.close();
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                db.execSQL(DROP_TABLE);
                onCreate(db);
            }catch (Exception ignore) {
            }
        }
    }

}

日志

Exception 03-17 23:38:07.130: E/StrictMode(1726): A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
03-17 23:38:07.130: E/StrictMode(1726): java.lang.Throwable: Explicit termination method 'close' not called
03-17 23:38:10.100: E/AndroidRuntime(2371): FATAL EXCEPTION: main
03-17 23:38:10.100: E/AndroidRuntime(2371): Process: com.example.android.passencrypt, PID: 2371
03-17 23:38:10.100: E/AndroidRuntime(2371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.passencrypt/com.example.android.passencrypt.MainBoard}: java.lang.UnsupportedOperationException

1 个答案:

答案 0 :(得分:1)

问题是当我尝试将对象添加到列表中时因为这个List dataList = Collections.emptyList();