尝试从解析中获取数据时获取indexOutOfBoundsException

时间:2016-01-13 19:01:37

标签: java android parse-platform arraylist indexoutofboundsexception

我正在尝试从解析中获取一些数据,但由于此错误,我的应用每次都会崩溃:java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.abc.xyz.AcceptARequest$1$1.done(AcceptARequest.java:157)

这是AcceptARequest.java(我从解析中获取数据)文件的代码:

public class AcceptARequest extends Fragment{

    private OnFragmentInteractionListener mListener;

    public List<ListContentAAR> listContentAARs;
    public RecyclerView recyclerView;

    ImageView hPicAccept;
    TextView hDescriptionAccept, currentCoordinatesTagAccept, currentLatAccept, currentLngAccept, post_date, post_time, posted_by;
    String hDescriptionAcceptS, currentCoordinatesTagAcceptS, currentLatAcceptS, currentLngAcceptS, post_dateS, post_timeS, posted_byS;
    Button btn_accept, btn_share;
    LinearLayout latContainerAccept, lngContainerAccept, dateTimeContainer;

    ParseQuery<ParseObject> query;
    String hDescription, cLat, cLng;

    public AcceptARequest() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment

        View rootView = inflater.inflate(R.layout.fragment_accept_a_request, container, false);

        listContentAARs = new ArrayList<>();

        hPicAccept = (ImageView) rootView.findViewById(R.id.h_pic_accept);
        hDescriptionAccept = (TextView) rootView.findViewById(R.id.h_description_accept);
        currentCoordinatesTagAccept = (TextView) rootView.findViewById(R.id.currentCoordinatesTagAccept);
        currentLatAccept = (TextView) rootView.findViewById(R.id.currentLatAccept);
        currentLngAccept = (TextView) rootView.findViewById(R.id.currentLngAccept);
        btn_accept = (Button) rootView.findViewById(R.id.btn_accept);
        btn_share = (Button) rootView.findViewById(R.id.btn_share);
        latContainerAccept = (LinearLayout) rootView.findViewById(R.id.latContainerAccept);
        lngContainerAccept = (LinearLayout) rootView.findViewById(R.id.lngContainerAccept);
        dateTimeContainer = (LinearLayout) rootView.findViewById(R.id.date_time_container);
        post_date = (TextView) rootView.findViewById(R.id.post_date);
        post_time = (TextView) rootView.findViewById(R.id.post_time);
        posted_by = (TextView) rootView.findViewById(R.id.posted_by);

        hDescriptionAcceptS = hDescriptionAccept.getText().toString();
        currentCoordinatesTagAcceptS = currentCoordinatesTagAccept.getText().toString();
        currentLatAcceptS = currentLatAccept.getText().toString();
        currentLngAcceptS = currentLngAccept.getText().toString();
        post_dateS = post_date.getText().toString();
        post_timeS = post_time.getText().toString();
        posted_byS = posted_by.getText().toString();

        currentCoordinatesTagAccept.setVisibility(View.INVISIBLE);
        currentLatAccept.setVisibility(View.INVISIBLE);
        currentLngAccept.setVisibility(View.INVISIBLE);
        latContainerAccept.setVisibility(View.INVISIBLE);
        lngContainerAccept.setVisibility(View.INVISIBLE);

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                query = ParseQuery.getQuery("HomelessDetails");
                query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
                query.addDescendingOrder("createdAt");
                query.findInBackground(new FindCallback<ParseObject>() {
                    @Override
                    public void done(List<ParseObject> list, ParseException e) {
                        if (e == null) {
                            currentCoordinatesTagAccept.setVisibility(View.VISIBLE);
                            currentLatAccept.setVisibility(View.VISIBLE);
                            currentLngAccept.setVisibility(View.VISIBLE);
                            latContainerAccept.setVisibility(View.VISIBLE);
                            lngContainerAccept.setVisibility(View.VISIBLE);

                            hDescriptionAcceptS = list.get(1).getString("hDescription");
                            hDescriptionAccept.setText(hDescriptionAcceptS);

                            currentLatAcceptS = list.get(1).getString("hLatitude");
                            currentLatAccept.setText(currentLatAcceptS);

                            currentLngAcceptS = list.get(1).getString("hLongitude");
                            currentLngAccept.setText(currentLngAcceptS);



                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                            builder.setMessage(e.getMessage());
                            builder.setPositiveButton(android.R.string.ok, null);
                            AlertDialog dialog = builder.create();
                            dialog.show();
                        }
                    }
                });
            }
        }, 1000);

        recyclerView = (RecyclerView) rootView.findViewById(R.id.accept_request_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
            @Override
            public void onClick(View view, int position) {
                Button btnAccept = (Button) view.findViewById(R.id.btn_accept);
                btnAccept.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                        builder.setMessage(R.string.request_accepted_txt);
                        builder.setPositiveButton("Navigate me", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(getActivity(), "Navigating you to the needy...", Toast.LENGTH_LONG).show();
                            }
                        });
                        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Toast.makeText(getActivity(), "The help-request has been rejected.", Toast.LENGTH_LONG).show();
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();
                    }
                });
                Button btnShare = (Button) view.findViewById(R.id.btn_share);
                btnShare.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        // share the help-request here.
                    }
                });
            }

            @Override
            public void onLongClick(View view, int position) {

            }
        }));

        initializeAdapter();

        return rootView;
    }

    private void initializeAdapter(){
        RVAdapterAAR adapter = new RVAdapterAAR(listContentAARs);
        recyclerView.setAdapter(adapter);
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

     public void itemClicked(View view, int position) {

    }

    class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

        private GestureDetector gestureDetector;
        private ClickListener clickListener;

        public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
            this.clickListener = clickListener;
            gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    return true;
                }

                @Override
                public void onLongPress(MotionEvent e) {
                    View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                    if (child != null && clickListener != null) {
                        clickListener.onLongClick(child, recyclerView.getChildPosition(child));
                    }
                    super.onLongPress(e);
                }
            });
        }

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            View child = rv.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
                clickListener.onClick(child, rv.getChildPosition(child));
            }
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    }

    public static interface ClickListener {
        public void onClick(View view, int position);
        public void onLongClick(View view, int position);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

我不知道这里出了什么问题。

请告诉我。

如果问题似乎格式错误,请合作,我还在学习发表好问题。

2 个答案:

答案 0 :(得分:2)

您正在尝试访问其中没有任何元素的ArrayList。 SO list.get(1)将无效。

确保正确填充ArrayList。

答案 1 :(得分:-1)

@Hammad: 您获得的错误是因为您使用的数组列表中不包含任何元素/对象。

您需要使用FROM ubuntu:14.04 ENV DEBIAN_FRONTEND=noninteractive RUN apt-get -y purge php.* RUN apt-get update RUN apt-get upgrade -y RUN apt-get install -y wget RUN apt-get install -y build-essential RUN wget http://xmlsoft.org/sources/libxml2-2.8.0.tar.gz RUN tar -xvf libxml2-2.8.0.tar.gz WORKDIR libxml2-2.8.0 RUN ./configure && make && make install RUN apt-get install -y make # build php RUN wget http://museum.php.net/php5/php-5.4.0.tar.bz2 RUN tar -xvf php-5.4.0.tar.bz2 WORKDIR php-5.4.0 RUN ./configure && make RUN make install # Replace 1000 with your user / group id RUN export uid=1000 gid=1000 # −p, −−parents -> no error if existing, make parent directories as needed RUN mkdir -p /home/developer (或根据您的偏好)方法填充 arraylist。