我有一个 listView 可以让用户跟踪某些内容并删除项目行。当我按下删除按钮时,它显示问题如下。我已经检查并在网上找到了解决方案,但我无法理解他们的解决方法,也许我的情况与他们不同。对这个问题有什么想法吗??
Logcat 输出(错误):
java.lang.ArrayIndexOutOfBoundsException: length=1; index=-1
at java.util.ArrayList.get(ArrayList.java:306)
at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
at com.example.android.project.ThirdActivity$3$2
.onClick(ThirdActivity.java:295)
at com.android.internal.app.AlertController$ButtonHandler
.handleMessage(AlertController.java:174)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller
.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
第三活动.java
friend = new ArrayList<String>(Arrays.asList(result.split("[*]")));
public void onListItemClick(ListView parent, View v, final int position, long id){
AlertDialog.Builder adb = new AlertDialog.Builder(ThirdActivity.this);
adb.setItems(item, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(item[which].equals("Track Location")){
...
}else{
AlertDialog.Builder adb=new AlertDialog.Builder(ThirdActivity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete " + (String) lv.getItemAtPosition(position));
final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, final int which) {
new Thread(){
public void run(){
try{
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.kryptoquest.com/tracker/remove.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
Log.d("12345678",user);
nameValuePairs.add(new BasicNameValuePair("Targetname", (String) lv.getItemAtPosition(position)));
nameValuePairs.add(new BasicNameValuePair("Username", user));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
is = response.getEntity().getContent();
}catch(Exception e){
Log.e("log_tag", "Error:"+e.toString());
}
//convert response to string
try{
reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Log.d("test",sb.toString());
is.close();
result = sb.toString();
runOnUiThread(new Runnable()
{
public void run()
{
Toast.makeText(ThirdActivity.this, result + lv.getItemAtPosition(position), Toast.LENGTH_LONG).show();
}
});
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
}
}.start();
String delete = (String) ((lv.getAdapter()).getItem(which));
friend.remove(delete);
friend.trimToSize();
adapter.notifyDataSetChanged();
}});
adb.show();
}
}
});
AlertDialog ad = adb.create();
ad.show();
}