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) 在 java.util.ArrayList.get(ArrayList.java:308) 在 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 - 尝试从解析中获取数据时获取 indexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34774930/

相关文章:

java - 在线程调度程序中为已等待和通知的线程设置优先级

java网格/板for循环: what row is empty and nearest?

android - 多个 Activity 上的 AsyncTask?

ios - 我正在尝试在我的应用程序中实现搜索栏,以便我可以搜索从 parse.com 检索到的对象

ios - PFQuery 匹配查询不工作 Parse.com

java - SWT:如何在 OS X 上正确处理应用程序事件

java - 使用Java使用正则表达式查找更大字符串的子字符串

java - 单击在 Listitem Listview android 上不起作用

java - 如何在 Jetpack 导航中停止 fragment 重新加载/重新创建

ios - Parse.com IAP : How to switch between production and sandbox mode?