无法从SQLite中删除应用程序崩溃

时间:2014-02-22 19:04:39

标签: android sqlite

我要从我的表中删除一行,但我的应用程序崩溃了。 正如你所看到的,我尝试了不同的方法,但它仍然崩溃。

MainActivity(使用SQL显示数据库中的表)

public class MainActivity extends ListActivity {

ArrayAdapter<String> adapter;
ArrayList<String> listItems;
SQLiteDatabase db;
Cursor cursor;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.db = (new DBManager(this, "myDB", null, 1)).getWritableDatabase();

    listItems = new ArrayList<String>();
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listItems);
    setListAdapter(adapter);

    this.db = (new DBManager(this, "myDB", null, 1)).getWritableDatabase();
    this.db.execSQL("CREATE TABLE IF NOT EXISTS items(id INTEGER, name VARCHAR,quantity INTEGER,catgory VARCHAR,warehouse VARCHAR);");

    this.cursor = this.db.rawQuery("SELECT * FROM items;", null);
    for(cursor.moveToFirst();cursor.getCount()>0 &&!cursor.isAfterLast() ;cursor.moveToNext()){
        listItems.add(cursor.getInt(0)+","+cursor.getString(1)+","+cursor.getInt(2)+","+cursor.getString(3)+","+cursor.getString(4));
    }
    adapter.notifyDataSetChanged();
    db.close(); 
}

public void additems(View v) {
    Intent addLayout = new Intent(MainActivity.this,AddItems.class);
    startActivity(addLayout);
    finish();
}

public void DelSelected(View v) {
    SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
    int itemCount = getListView().getCount();
    for(int i=itemCount-1; i >= 0; i--){
        if(checkedItemPositions.get(i)){  
           String item = (String) getListView().getItemAtPosition(i);

           //int selectedID = Integer.parseInt(item1[0]);
           //Toast.makeText(this, Integer.toString(selectedID), Toast.LENGTH_SHORT).show();

           //this.db.execSQL("DELETE FROM items WHERE id="+selectedID+";");
           //this.db.execSQL("DELETE FROM items WHERE id = 1001;");
           this.db.delete("items", "id=1001", null);
           adapter.remove(listItems.get(i));
        }
    }
    checkedItemPositions.clear();
    adapter.notifyDataSetChanged();
}

AddItems(使用SQL向Data Base添加项目)

    public void insertRow(int ID, String name , int Quan , String catgory, String warehouse) {
    this.db.execSQL("INSERT INTO items VALUES ("+ID+" ,'" + name + "',"+Quan+",'"+catgory+"','"+warehouse+"');");
}
public void add(View v) {
    //exemples for testing
    this.insertRow(1001, "42 inch", 1, "TV", "Electricity");
    this.insertRow(1002, "32 inch", 2, "TV", "Electricity");
    this.insertRow(2001, "Tomato", 1, "Vegetables", "Food");
    this.insertRow(2002, "Cucumbers", 2, "Vegetables", "Food");
    this.insertRow(3001, "Knife", 1, "Tools", "Kitchen");
    this.insertRow(3002, "Fork", 2, "Tools", "Kitchen");
    this.insertRow(4001, "Nana", 1, "Plants", "Garden");
    this.insertRow(4002, "Limon", 2, "Plants", "Garden");

    Intent backToMain = new Intent(AddItems.this,MainActivity.class);
    startActivity(backToMain);
    finish();
}

这是错误LOGCAT

02-22 21:20:42.903:E / AndroidRuntime(28294):致命异常:主要 02-22 21:20:42.903:E / AndroidRuntime(28294):java.lang.IllegalStateException:无法执行活动的方法 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.view.View $ 1.onClick(View.java:3606) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.view.View.performClick(View.java:4211) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.view.View $ PerformClick.run(View.java:17446) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.os.Handler.handleCallback(Handler.java:725) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.os.Handler.dispatchMessage(Handler.java:92) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.os.Looper.loop(Looper.java:153) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.app.ActivityThread.main(ActivityThread.java:5338) 02-22 21:20:42.903:E / AndroidRuntime(28294):at java.lang.reflect.Method.invokeNative(Native Method) 02-22 21:20:42.903:E / AndroidRuntime(28294):at java.lang.reflect.Method.invoke(Method.java:511) 02-22 21:20:42.903:E / AndroidRuntime(28294):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833) 02-22 21:20:42.903:E / AndroidRuntime(28294):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 02-22 21:20:42.903:E / AndroidRuntime(28294):at dalvik.system.NativeStart.main(Native Method) 02-22 21:20:42.903:E / AndroidRuntime(28294):引起:java.lang.reflect.InvocationTargetException 02-22 21:20:42.903:E / AndroidRuntime(28294):at java.lang.reflect.Method.invokeNative(Native Method) 02-22 21:20:42.903:E / AndroidRuntime(28294):at java.lang.reflect.Method.invoke(Method.java:511) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.view.View $ 1.onClick(View.java:3601) 02-22 21:20:42.903:E / AndroidRuntime(28294):... 11更多 02-22 21:20:42.903:E / AndroidRuntime(28294):引起:java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteDatabase:/data/data/com.example.stockhendler/databases / MYDB 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55) 02-22 21:20:42.903:E / AndroidRuntime(28294):在android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1489) 02-22 21:20:42.903:E / AndroidRuntime(28294):at com.example.stockhendler.MainActivity.DelSelected(MainActivity.java:67) 02-22 21:20:42.903:E / AndroidRuntime(28294):... 14更多

1 个答案:

答案 0 :(得分:1)

您可以尝试:

this.db.delete("items", "id=?", new String[] { String.valueOf(selectedID) });

第三个参数必须是字符串数组。