我有一个带有图像和按钮的listview应用程序。但是,一旦我单击holder.RSVP并执行了RemoteDataTask,该应用程序就会运行,如果我尝试继续将列表视图向下滚动到其他图像,则该应用程序将崩溃。日志猫读取“无效索引3,java.util.Arraylist.throwIndexOutofBoundsException的大小为0”。我的猜测是该错误发生在内部PostComment类中(成功== 1)。

  public class ListViewAdapter4 extends BaseAdapter {

        public ListViewAdapter4(Context context,
                List<WorldPopulation2> worldpopulationlist) {
            this.context = context;
            this.worldpopulationlist = worldpopulationlist;
            inflater = LayoutInflater.from(context);
            this.arraylist = new ArrayList<WorldPopulation2>();
            this.arraylist.addAll(worldpopulationlist);
        }

        public class ViewHolder {
            TextView Username;
            TextView Filename;
            ImageView flag;
            ImageView success;
            TextView location;
            TextView message;
            ImageView event;
            Button button1;
            Layout mLayout;
            ImageButton RSVP;
            ImageButton done;
            TextView responses;
        }

        @Override
        public int getCount() {
            return worldpopulationlist.size();
        }

        @Override
        public Object getItem(int position) {
            return worldpopulationlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View view, ViewGroup parent) {
            final ViewHolder holder;
            if (view == null) {
                holder = new ViewHolder();
                view = inflater.inflate(R.layout.dialogprofile, null);
                // Locate the TextViews in listview_item.xml
                ParseUser currentUser = ParseUser.getCurrentUser();
                struser = currentUser.getUsername().toString();
                holder.RSVP = (ImageButton) view.findViewById(R.id.rsvp);
                holder.success = (ImageView) view.findViewById(R.id.humble);
                holder.Username = (TextView) view.findViewById(R.id.username);
                holder.location = (TextView) view.findViewById(R.id.location);
                holder.message = (TextView) view.findViewById(R.id.message1);
                holder.event = (ImageView) view.findViewById(R.id.imageView1);
                holder.done = (ImageButton) view.findViewById(R.id.done);
                holder.done.setVisibility(View.INVISIBLE);
                holder.responses = (TextView) view.findViewById(R.id.responses);
                view.setTag(holder);
            } else {

                holder = (ViewHolder) view.getTag();
            }

           });
           holder.RSVP.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                holder.done.setVisibility(View.VISIBLE);
                holder.RSVP.setVisibility(View.INVISIBLE);
                seconduser = worldpopulationlist.get(position).getUsername();
                seconduserfile = worldpopulationlist.get(position).getFilename();
                secondlocation = worldpopulationlist.get(position).getLocation();
                new RemoteDataTask().execute();

            }

           });

        private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
               // mProgressDialog.show();
            }


        @Override
        protected Void doInBackground(Void... params) {

                    worldpopulationlist = new ArrayList<WorldPopulation2>();
                    mCommentList = new ArrayList<HashMap<String, String>>();
                    JSONParser jParser = new JSONParser();
                    JSONObject json = jParser.getJSONFromUrl(READ_COMMENTS_URL);

                    try {

                        mComments = json.getJSONArray(TAG_POSTS);

                        Map<String, Object> example = new HashMap<String, Object>();

                        // looping through all posts according to the json object returned
                        for (int i = 0; i < mComments.length(); i++) {
                              JSONObject c = mComments.getJSONObject(i);

                            if (c.getString(TAG_USERNAME).contentEquals(struser)) {
                            username = c.getString(TAG_USERNAME);
                            filename = c.getString(TAG_FILENAME);

                            }

                        }
                        new PostComment().execute();
                    }catch (JSONException e) {
                        e.printStackTrace();
                    }
                    return null;


        }

        class PostComment extends AsyncTask<String, String, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                //pDialog.show();
            }

            @Override
            protected String doInBackground(String... args) {
                // TODO Auto-generated method stub
                // Check for success tag
                int success;

                try {
                    // Building Parameters
                    message = username + " has RSVP'd to " + seconduser + "'s activity at " + secondlocation;
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("username", username));
                    params.add(new BasicNameValuePair("useronefile", filename));
                    params.add(new BasicNameValuePair("message", message));
                    params.add(new BasicNameValuePair("location", secondlocation));
                    params.add(new BasicNameValuePair("usernameone", seconduser));
                    params.add(new BasicNameValuePair("seconduserfile", seconduserfile));

                    Log.d("request!", "starting");

                    // Posting user data to script
                    JSONObject json = jsonParser.makeHttpRequest(POST_COMMENT_URL1,
                            "POST", params);

                    // full json response
                    Log.d("Post Comment attempt", json.toString());

                    // json success element
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {

                    } else {
                        Log.d("Comment Failure!", json.getString(TAG_MESSAGE));
                        return json.getString(TAG_MESSAGE);

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;



            }

            protected void onPostExecute(String file_url) {
                // dismiss the dialog once product deleted
                //pDialog.dismiss();
                if (file_url != null) {
                }

            }

        }
    }
    }


Logcat:

FATAL EXCEPTION: main
Process: com.example PID:
     java.lang.IndexOutOfBoundsException: Invalid index 2, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.chronospect.ListViewAdapter4.getView(ListViewAdapter4.java:130)
        at android.widget.AbsListView.obtainView(AbsListView.java:2255)
        at android.widget.ListView.makeAndAddView(ListView.java:1790)
        at android.widget.ListView.fillDown(ListView.java:691)
        at android.widget.ListView.fillGap(ListView.java:655)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5143)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4254)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:543)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
       at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

最佳答案

当您调用RemoteDataTask时,您正在重置worldpopulationlist,因此一旦被调用,列表视图中的项目数将变为零,并且您会得到Exception

09-29 20:30