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

原文 标签 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);
    }
}


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

请告诉我。

如果问题的格式似乎不正确,请配合,我仍在学习发布好的问题。

最佳答案

您正在尝试访问其中没有任何元素的ArrayList。所以list.get(1)无法正常工作。

确保正确填充了ArrayList。

相关文章:

java - Sqlite:独特使我的数据库发疯

android - Android i.MX6(Udoo开发板)上的OpenCL

java - Android,用Java将邮政编码转换为经度和纬度的最快,最有效的方法是什么?

java - 编译失败:找不到模块:org.apache.logging.log4j

java - 这是在Java中创建允许任何来源的CORS过滤器的好方法吗?

ios - 解析查询包装错误,在设备上的模拟器崩溃时可以正常工作

ios - 使用Parse注册后,performSegueWithIdentifier不起作用

ios - 从解析查询中保存PFObjects

java - 将笛卡尔5D和角2D转换为Lat Long Alt