点击“回收者视图”子项

时间:2018-07-14 04:20:54

标签: java android android-recyclerview

一旦用户在我的应用中点击recyclerview项目,我就会尝试开始一项新的活动。但是不幸的是它崩溃了。我已将以下所有相关代码包括在内,以进行诊断。如果我可以在这里获得一些帮助,将会很有帮助。另外,在哪里必须添加onClick方法来启动new intent?目前,我已将意图放在onInterceptTouchEvent()方法下。

public class view extends AppCompatActivity {
List<DataAdapter> DataAdapterClassList;

RecyclerView recyclerView;

RecyclerView.LayoutManager recyclerViewlayoutManager;

RecyclerView.Adapter recyclerViewadapter;

ProgressBar progressBar;

JsonArrayRequest jsonArrayRequest ;

ArrayList<String> EmployeeNames;

RequestQueue requestQueue ;

String HTTP_SERVER_URL = "http://andoirdapp.000webhostapp.com/EmployeeDetails.php";

View ChildView ;

List<String> IdList = new ArrayList<>();

int RecyclerViewClickedItemPOS ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_view);

    DataAdapterClassList = new ArrayList<>();

    EmployeeNames = new ArrayList<>();

    recyclerView = findViewById(R.id.recyclerView1);

    progressBar = findViewById(R.id.progressBar);

    recyclerView.setHasFixedSize(true);

    recyclerViewlayoutManager = new LinearLayoutManager(this);

    recyclerView.setLayoutManager(recyclerViewlayoutManager);

    // JSON data web call function call from here.
    JSON_WEB_CALL();

    //RecyclerView Item click listener code starts from here.
    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        GestureDetector gestureDetector = new GestureDetector(view.this, new GestureDetector.SimpleOnGestureListener() {

            @Override public boolean onSingleTapUp(MotionEvent motionEvent) {

                return true;
            }

        });
        @Override
        public boolean onInterceptTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

            ChildView = Recyclerview.findChildViewUnder(motionEvent.getX(), motionEvent.getY());

            if(ChildView != null && gestureDetector.onTouchEvent(motionEvent)) {

                //Getting RecyclerView Clicked item value.
                RecyclerViewClickedItemPOS = Recyclerview.getChildAdapterPosition(ChildView);

                Intent intent = new Intent (view.this,ShowSingleRecordActivity.class);

                intent.putExtra("ListViewValue", IdList.get(RecyclerViewClickedItemPOS));

                startActivity(intent);

                finish();

                //Printing RecyclerView Clicked item clicked value using Toast Message.
                Toast.makeText(view.this, EmployeeNames.get(RecyclerViewClickedItemPOS), Toast.LENGTH_LONG).show();

            }

            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView Recyclerview, MotionEvent motionEvent) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });

}

public void JSON_WEB_CALL(){

    jsonArrayRequest = new JsonArrayRequest(HTTP_SERVER_URL,

            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(jsonArrayRequest);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

    for(int i = 0; i<array.length(); i++) {

        DataAdapter GetDataAdapter2 = new DataAdapter();

        JSONObject json = null;
        try {
            json = array.getJSONObject(i);

            //GetDataAdapter2.setId(json.getInt("id"));

            GetDataAdapter2.setEmployeeNumber(json.getString("EmployeeNumber"));

            GetDataAdapter2.setEmployeeName(json.getString("EmployeeName"));

            //Adding subject name here to show on click event.
            EmployeeNames.add(json.getString("EmployeeName"));

            GetDataAdapter2.setDesignation(json.getString("Designation"));

            GetDataAdapter2.setDepartment(json.getString("Department"));

            GetDataAdapter2.setGrade(json.getString("Grade"));

            GetDataAdapter2.setSection(json.getString("Section"));

            GetDataAdapter2.setApplicationDetails(json.getString("ApplicationDetails"));

            GetDataAdapter2.setDateOfAppointment(json.getString("DateOfAppointment"));

            GetDataAdapter2.setSeniorityNumber(json.getString("SeniorityNumber"));

            GetDataAdapter2.setSeniorityGrade(json.getString("SeniorityGrade"));

            GetDataAdapter2.setSeniorityDivision(json.getString("SeniorityDivision"));

            GetDataAdapter2.setRequestIsFor(json.getString("RequestIsFor"));

            GetDataAdapter2.setPresentAddress(json.getString("PresentAddress"));

            GetDataAdapter2.setContactNumber(json.getString("ContactNumber"));

            GetDataAdapter2.setRemarks(json.getString("Remarks"));

            GetDataAdapter2.setQuarterPreference(json.getString("QuarterPreference"));



        }
        catch (JSONException e)
        {

            e.printStackTrace();
        }

        DataAdapterClassList.add(GetDataAdapter2);

    }

    progressBar.setVisibility(View.GONE);

    recyclerViewadapter = new RecyclerViewAdapter(DataAdapterClassList, this);

    recyclerView.setAdapter(recyclerViewadapter);
}
}

错误消息:

07-14 09:32:26.467 9306-9306/com.kavyabarnadhyahazarika.quarterallotmentapp E/InputEventReceiver: Exception dispatching input event.
07-14 09:32:26.468 9306-9306/com.kavyabarnadhyahazarika.quarterallotmentapp E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
07-14 09:32:26.469 9306-9306/com.kavyabarnadhyahazarika.quarterallotmentapp E/MessageQueue-JNI: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.kavyabarnadhyahazarika.quarterallotmentapp.view$1.onClick(view.java:177)
        at com.kavyabarnadhyahazarika.quarterallotmentapp.view$1RecyclerTouchListener.onInterceptTouchEvent(view.java:153)
        at android.support.v7.widget.RecyclerView.dispatchOnItemTouch(RecyclerView.java:2916)
        at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:3044)
        at android.view.View.dispatchTouchEvent(View.java:10039)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2628)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:417)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3198)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:379)
        at android.view.View.dispatchPointerEvent(View.java:10261)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4500)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4365)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3905)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3958)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3924)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4051)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3932)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4108)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3905)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3958)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3924)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3932)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3905)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6309)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6283)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6244)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6415)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:187)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
        at android.os.Looper.loop(Looper.java:142)
        at android.app.ActivityThread.main(ActivityThread.java:6239)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
07-14 09:32:26.471 9306-9306/com.kavyabarnadhyahazarika.quarterallotmentapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kavyabarnadhyahazarika.quarterallotmentapp, PID: 9306
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:411)
        at com.kavyabarnadhyahazarika.quarterallotmentapp.view$1.onClick(view.java:177)
        at com.kavyabarnadhyahazarika.quarterallotmentapp.view$1RecyclerTouchListener.onInterceptTouchEvent(view.java:153)
        at android.support.v7.widget.RecyclerView.dispatchOnItemTouch(RecyclerView.java:2916)
        at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:3044)
        at android.view.View.dispatchTouchEvent(View.java:10039)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2628)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2307)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2634)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2321)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:417)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1808)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3198)
        at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:379)
        at android.view.View.dispatchPointerEvent(View.java:10261)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4500)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4365)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3905)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3958)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3924)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4051)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3932)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4108)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3905)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3958)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3924)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3932)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3905)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6309)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6283)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6244)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6415)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:187)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
        at android.os.Looper.loop(Looper.java:142)
        at android.app.ActivityThread.main(ActivityThread.java:6239)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)

2 个答案:

答案 0 :(得分:1)

您永远不会将任何条目添加到数组Version中,因此,当您尝试使用IdList从数组中获取条目时,会出现以下错误:

IdList.get(...)

该错误消息告诉您您的数组为空(大小= 0),并且您正在尝试获取第一个条目(索引0)。它甚至可以告诉您发生在哪一行(在您的java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 通话中的第177行)

onClick

答案 1 :(得分:1)

例外是:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

非常清楚,您尝试访问了列表中不可用的某些索引。我调查了view.java,发现了以下故障。您正在尝试从列表中获取数据而未验证Arraylist(null还是empty)

intent.putExtra("ListViewValue", IdList.get(RecyclerViewClickedItemPOS));行中的错误

您需要先检查:

if(IdList.get(RecyclerViewClickedItemPOS)!= null && IdList.get(RecyclerViewClickedItemPOS) > 0)
{
intent.putExtra("ListViewValue", IdList.get(RecyclerViewClickedItemPOS));
}