我正在为Android编写应用程序。但是,我在sqlite中删除记录时遇到问题。你能帮我解决问题吗?当我单击删除按钮时,LogCat将出现问题:
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
这是代码:
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Delete:
int itemId = item.getItemId();
AdapterView.AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
dm.delete(menuinfo.id);
list.remove(menuinfo.position);
popupToast(" records deleted");
displayListScreen();
return true;
default:
return super.onContextItemSelected(item);
}
}`
这是数据库代码:
public void delete(long id) {
String where = "id = " + id;
db.delete(TABLE_NAME, where, null);
}
... Editted
对不起你们,我第一次参加。现在我会把它发给你们。 logcat的
10-28 17:51:03.586: D/libEGL(3831): loaded /system/lib/egl/libEGL_adreno200.so
10-28 17:51:03.586: D/libEGL(3831): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
10-28 17:51:03.586: D/libEGL(3831): loaded /system/lib/egl/libGLESv2_adreno200.so
10-28 17:51:03.596: I/Adreno200-EGL(3831): <qeglDrvAPI_eglInitialize:294>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB.04.01.01.00.036_msm8960_JB_CL2644550_release_AU (CL2644550)
10-28 17:51:03.596: I/Adreno200-EGL(3831): Build Date: 07/31/12 Tue
10-28 17:51:03.596: I/Adreno200-EGL(3831): Local Branch:
10-28 17:51:03.596: I/Adreno200-EGL(3831): Remote Branch: quic/master
10-28 17:51:03.596: I/Adreno200-EGL(3831): Local Patches: NONE
10-28 17:51:03.596: I/Adreno200-EGL(3831): Reconstruct Branch: AU_LINUX_ANDROID_JB.04.01.01.00.036 + NOTHING
10-28 17:51:03.626: D/OpenGLRenderer(3831): Enabling debug mode 0
10-28 17:51:13.856: D/AndroidRuntime(3831): Shutting down VM
10-28 17:51:13.856: W/dalvikvm(3831): threadid=1: thread exiting with uncaught exception (group=0x415f2930)
10-28 17:51:13.896: E/AndroidRuntime(3831): FATAL EXCEPTION: main
10-28 17:51:13.896: E/AndroidRuntime(3831): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-28 17:51:13.896: E/AndroidRuntime(3831): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-28 17:51:13.896: E/AndroidRuntime(3831): at java.util.ArrayList.remove(ArrayList.java:399)
10-28 17:51:13.896: E/AndroidRuntime(3831): at example.coursework.MainActivity.onContextItemSelected(MainActivity.java:135)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.app.Activity.onMenuItemSelected(Activity.java:2566)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3899)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:941)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.widget.AbsListView$1.run(AbsListView.java:3423)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.os.Handler.handleCallback(Handler.java:725)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.os.Handler.dispatchMessage(Handler.java:92)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.os.Looper.loop(Looper.java:137)
10-28 17:51:13.896: E/AndroidRuntime(3831): at android.app.ActivityThread.main(ActivityThread.java:5227)
10-28 17:51:13.896: E/AndroidRuntime(3831): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 17:51:13.896: E/AndroidRuntime(3831): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-28 17:51:13.896: E/AndroidRuntime(3831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-28 17:51:13.896: E/AndroidRuntime(3831): at dalvik.system.NativeStart.main(Native Method)
10-28 17:51:15.158: I/Process(3831): Sending signal. PID: 3831 SIG: 9
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
TextView selection;
public int idToModify;
DataManipulator dm;
List<String[]> list = new ArrayList<String[]>();
List<String[]> names2 = null;
String[] stg1;
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
// Search EditText
EditText inputSearch;
// ArrayList for Listview
ArrayList<HashMap<String, String>> productList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View button1Click = findViewById(R.id.addButton);
button1Click.setOnClickListener(this);
dm = new DataManipulator(this);
names2 = dm.selectAll();
stg1 = new String[names2.size()];
int x = 0;
String stg;
for (String[] name : names2) {
stg = "Name: " + name[1] + "\n" + "Gender: " + name[2] + "\n"
+ "Height: " + name[3] + "\n" + "Age: " + name[4] + "\n"
+ "Hair Color: " + name[5] + "\n" + "Comment: " + name[6]
+ "\n" + "Phone Number: " + name[7];
stg1[x] = stg;
x++;
}
// Listview Data
lv = (ListView) findViewById(R.id.list_view1);
inputSearch = (EditText) findViewById(R.id.inputSearch);
registerForContextMenu(lv);
// Adding items to listview
adapter = new ArrayAdapter<String>(this, R.layout.list_item,
R.id.product_name, stg1);
lv.setAdapter(adapter);
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
// Create Context Menu
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
menu.setHeaderTitle(stg1[info.position]);
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
// When User Selected Contexted Menu
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Delete:
int itemId = item.getItemId();
AdapterView.AdapterContextMenuInfo menuinfo = (AdapterContextMenuInfo) item.getMenuInfo();
dm.delete(menuinfo.id);
list.remove(menuinfo.position);
popupToast(" records deleted");
displayListScreen();
return true;
default:
return super.onContextItemSelected(item);
}
}
/*private void deleteRecords() {
// Create and display the Alert dialog
new AlertDialog.Builder(this)
.setMessage("Do you really want to delete selected records?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
int position = 0;
String[] delete = names2.get(position);
String idString = delete[(position)];
long idLong = Long.valueOf(idString);
dm.delete(idLong);
popupToast(" records deleted");
displayListScreen();
}
}).show();
}*/
private void popupToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
private void displayListScreen() {
startActivity(new Intent(this, MainActivity.class));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.addButton:
Intent i = new Intent(this, Coursework.class);
startActivity(i);
break;
}
}
}
抱歉,非常感谢你
答案 0 :(得分:1)
您的问题缺乏详细信息,但是,您发布的代码中唯一的地方可能会出现IndexOutOfBoundsException
例外情况:
list.remove(menuinfo.position);
假设list
是java.util.List
的一个实例,您将收到此异常,因为menuinfo.position
小于零或大于list
中的元素数。
立即修复:
if (menuinfo.position < 0 || menuinfo.position >= list.size()) {
// Warning, invalid position
} else {
list.remove(menuinfo.position);
}