尝试使用自动完成文本重新查询已关闭的光标

时间:2017-09-28 08:23:42

标签: java android mysql sqlite

java.lang.IllegalStateException: trying to requery an already closed cursor

我有2个活动,第2个用于查看数据库中的数据。

第一项活动 enter image description here

我可以在没有任何错误的情况下向表中添加数据,我可以从第二个活动中查看它们,该活动从db中的某个表中获取所有数据,并使用数组适配器将它们列在listview中。当我删除一行,然后查看列表时出现问题。然后回到第一个活动,突然出现错误并强行关闭....

这是我删除行的方法,我在扩展SQLiteOpenHelper的类中实现了这个。

public Integer deleteCustomer(Integer id) {
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(CUSTOMERS_TABLE_NAME,
            PERSON_COLUMN_ID + " = ? ",
            new String[] { Integer.toString(id) });
}

这是我使用asynctask

从第二个活动中的db获取数据的方法
Cursor c = db.getAllCustomerInfoFromCustomersTable();

        txtName  = new String[c.getCount()];
        txtTown  = new String[c.getCount()];
        txtTel  = new String[c.getCount()];
        txtId = new String[c.getCount()];
        c.moveToFirst();
        for(int a = 0; a < c.getCount(); a++){
            txtName[a] = c.getString(c.getColumnIndexOrThrow("custname"));
            txtTown[a] = c.getString(c.getColumnIndexOrThrow("town"));
            txtTel[a] = c.getString(c.getColumnIndexOrThrow("tel"));
            txtId[a] = c.getString(c.getColumnIndexOrThrow("_id"));
            c.moveToNext();
        }
        c.close();

它完美无缺。

这是我在自动填充的第一个活动中使用的类,

class ItemAutoTextAdapter extends CursorAdapter implements android.widget.AdapterView.OnItemClickListener{

    private AutoCompleteDbAdapter mDbHelper;

    public ItemAutoTextAdapter(AutoCompleteDbAdapter dbHelper, Context context){
        super(context, null);
        mDbHelper = dbHelper;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }

        Cursor cursor = mDbHelper.getMatchingStates(
                (constraint != null ? constraint.toString() : null));

        return cursor;
    }


    @Override
    public CharSequence convertToString(Cursor cursor) {
        final int columnIndex = cursor.getColumnIndexOrThrow("custname");
        final String str = cursor.getString(columnIndex);
        return str;
    }


    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final String text = (String)convertToString(cursor);
        ((TextView) view).setText(text);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final View view =
                inflater.inflate(android.R.layout.simple_dropdown_item_1line,
                        parent, false);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
        // Get the cursor, positioned to the corresponding row in the result set
        Cursor cursor = (Cursor) listView.getItemAtPosition(position);

        // Get the state's capital from this row in the database.
        String name = cursor.getString(cursor.getColumnIndexOrThrow("custname"));
        String town = cursor.getString(cursor.getColumnIndexOrThrow("town"));
        String tel = cursor.getString(cursor.getColumnIndexOrThrow("tel"));
        selectedID = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));

        // Update the parent class's TextView
        //mStateCapitalView.setText(capital);
        eName.setText(name);
        eTown.setText(town);
        eTel.setText(tel);


        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
    }
}

我在onCreate方法中初始化它,像这样,

void setShit(){
    dbAdapter = new AutoCompleteDbAdapter(this);
    ItemAutoTextAdapter adapter = this.new ItemAutoTextAdapter(dbAdapter, getBaseContext());
    mStateNameView.setAdapter(adapter);
    mStateNameView.setOnItemClickListener(adapter);
}

我尝试过的步骤

@Override
public void onPause(){
    super.onPause();
    dbAdapter.close();
}

@Override
public void onResume(){
    super.onResume();
    setShit();
}
不幸的是,他们没有工作,错误不断发生。

再次  当我删除表格中的一行时出现错误,然后按查看按钮,然后显示表格中的剩余行。然后我按回按钮,app力关闭。 只有在我编辑或删除一行时才会发生这种情况。

编辑方法,

public boolean updateCustomer(Integer id, String name, String town, String tel) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(CUSTOMERS_NAME, name);
    contentValues.put(CUSTOMERS_TOWN, town);
    contentValues.put(CUSOMERS_TEL, tel);
    db.update(CUSTOMERS_TABLE_NAME, contentValues, PERSON_COLUMN_ID + " = ? ", new String[] { Integer.toString(id) } );
    return true;
}

完整堆栈跟踪

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.ovnis.sscattered.billiingapp, PID: 30212
                                                                              java.lang.RuntimeException: Unable to resume activity {com.ovnis.sscattered.billiingapp/com.ovnis.sscattered.billiingapp.CustomersActivity}: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@eb8fa5e
                                                                                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3469)
                                                                                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3509)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6236)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
                                                                               Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor  android.database.sqlite.SQLiteCursor@eb8fa5e
                                                                                  at android.app.Activity.performRestart(Activity.java:6757)
                                                                                  at android.app.Activity.performResume(Activity.java:6780)
                                                                                  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3446)
                                                                                  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3509) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1557) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6236) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781) 

0 个答案:

没有答案