gpt4 book ai didi

java - 单击 Recycler View Child Item 启动新 Intent

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:02 25 4
gpt4 key购买 nike

我正在尝试在用户点击我应用中的 recyclerview 项目后开始一项新 Activity 。但不幸的是它崩溃了。我已经在下面包含了所有相关代码以供诊断。如果我能在这里得到一些帮助,那将会很有帮助。另外,我必须在哪里添加 onClick 方法来启动 new intent?目前,我已将 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)

最佳答案

您永远不会向数组 IdList 添加任何条目,因此当您尝试使用 IdList.get(...) 从中获取条目时,您会收到此错误:

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

错误消息告诉您您的数组为空(大小 = 0)并且您正在尝试获取第一个条目(索引 0)。它甚至会告诉您它发生在哪一行(第 177 行,在您的 onClick 调用中)

at java.util.ArrayList.get(ArrayList.java:411)
at com.kavyabarnadhyahazarika.quarterallotmentapp.view$1.onClick(view.java:177)

关于java - 单击 Recycler View Child Item 启动新 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51335201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com