listview - java.lang. ArrayIndexOutOfBoundsException: length=1 ;index= 1

  显示原文与译文双语对照的内容
0 0

我们已经在网站上找到了一些问题,但我无法理解问题的解决方式,也许我的情况不同。 有什么问题?

Logcat output(Error):


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)

ThirdActivity.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(); 
 }

时间: 原作者:

0 0

if(item[which].equals("Track Location"))

" item"是你提交给 Adapter的数据集,你应该使用 position 而不是 which

原作者:
0 0

替换


String delete = (String) ((lv.getAdapter()).getItem(which));


String delete = (String) ((lv.getAdapter()).getItem(position));

...