我有一个解析JSON数据并将其显示在回收者视图中的应用程序。如果用户在“回收者”视图中单击某个项目,则会将他们定向到新活动,将JSON数据传输到该活动中。到目前为止,JSON数据已解析到回收器视图中,没有任何问题。当我单击一个对象时,该应用程序崩溃,并出现ArrayIndexOutOfBoundsException并将我定向到我的Recycler View Adapter类。显然我正在尝试访问索引-1。
RecyclerViewAdapter类:据说错误发生在第37行的OnClick方法中。
package com.example.gabe.politicianspulse;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class RefRvAdapter extends RecyclerView.Adapter<RefRvAdapter.MyViewHolder>{
private Context context;
private List<Refs> refsList;
public RefRvAdapter(){}
public RefRvAdapter(List<Refs> refList, Context context){
this.refsList = refList;
this.context = context;
}
@NonNull
@Override
public RefRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater mInflater = LayoutInflater.from(context);
view = mInflater.inflate(R.layout.ref_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
viewHolder.view_containers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent (context, SingleReferendum.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("referendumTitle", refsList.get(viewHolder.getAdapterPosition()).getRefTitle());
i.putExtra("referendumSubtitle", refsList.get(viewHolder.getAdapterPosition()).getRefSub());
i.putExtra("referendumUrl", refsList.get(viewHolder.getAdapterPosition()).getRefUrl());
context.startActivity(i);
}
});
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RefRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.refTitle.setText(refsList.get(i).getRefTitle());
myViewHolder.refSub.setText(refsList.get(i).getRefSub());
}
@Override
public int getItemCount() {
return refsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView refTitle, refSub;
LinearLayout view_containers;
public MyViewHolder(View itemView){
super(itemView);
refTitle = itemView.findViewById(R.id.ref_title);
refSub = itemView.findViewById(R.id.ref_sub);
view_containers = itemView.findViewById(R.id.view_container_ref);
}
}
}
SingleReferendum类:这是我希望将来自回收器视图的JSON数据加载到的类。
package com.example.gabe.politicianspulse;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class SingleReferendum extends AppCompatActivity {
private List<Refs> refList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_referendum);
refList = new ArrayList<>();
//retrieve data
String title = getIntent().getExtras().getString("referendumTitle");
String subtitle = getIntent().getExtras().getString("referendumSubtitle");
String url = getIntent().getExtras().getString("referendumUrl");
//initialize views
TextView title_tv = findViewById(R.id.title);
TextView subtitle_tv = findViewById(R.id.subtitle);
TextView url_tv = findViewById(R.id.url);
//set string values to each view
title_tv.setText(title);
subtitle_tv.setText(subtitle);
url_tv.setText(url);
}
}
为什么会出现此错误?这是什么意思?仅当我调用onClick方法时才会发生此崩溃。我遗漏了所有的布局文件,只是因为文件太多了,但是如果你们需要查看特定的布局文件,请告诉我。我感谢任何反馈或输入。