这是我的MainActivity。

public class MatchContests extends AppCompatActivity implements ItemClickListener {

RecyclerView recyclerView;
private DatabaseReference myref;

private ArrayList<ZgetsetContests> contestsList;
private AdapterContests adapterContests;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_match_contests);

    recyclerView = findViewById(R.id.recyclerView);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);
    adapterContests.setClickListener(this);

    myref = FirebaseDatabase.getInstance().getReference();

    contestsList = new ArrayList<>();
    ClearAll();
    GetContestDetails();

}

private void GetContestDetails()
{
    Query query = myref.child("contestsdetails").child("match1");
    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            ClearAll();
            for (DataSnapshot snapshot : dataSnapshot.getChildren())
            {
                ZgetsetContests zgetsetContests = new ZgetsetContests();

                zgetsetContests.setEntry(snapshot.child("entry").getValue().toString());
                zgetsetContests.setBonususage(snapshot.child("bonususage").getValue().toString());
                zgetsetContests.setFirstposition(snapshot.child("firstposition").getValue().toString());
                zgetsetContests.setMaintitle(snapshot.child("maintitle").getValue().toString());
                zgetsetContests.setTeamsallowed(snapshot.child("teamsallowed").getValue().toString());
                zgetsetContests.setTotalspots(snapshot.child("totalspots").getValue().toString());
                zgetsetContests.setPrizepool(snapshot.child("prizepool").getValue().toString());

                contestsList.add(zgetsetContests);
            }
            adapterContests = new AdapterContests(getApplicationContext(),contestsList);
            recyclerView.setAdapter(adapterContests);
            adapterContests.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

private void ClearAll()
{
    if (contestsList != null) {
        contestsList.clear();

        if (adapterContests != null){adapterContests.notifyDataSetChanged();}
    }
    contestsList = new ArrayList<>();
}

@Override
public void onClick(View v, int position) {

}
}


我的适配器类。

public class AdapterContests extends RecyclerView.Adapter<AdapterContests.ViewHolder> {

private static final String Tag = "RecyclerView";
private Context mContext;
private ArrayList<ZgetsetContests> contestsList;
private DatabaseReference dr;
private ItemClickListener clickListener;

public AdapterContests(Context mContext, ArrayList<ZgetsetContests> contestsList) {
    this.mContext = mContext;
    this.contestsList = contestsList;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.design_contests,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

    holder.entry.setText(contestsList.get(position).getEntry());
    holder.prizepool.setText(contestsList.get(position).getPrizepool());
    holder.firstposition.setText(contestsList.get(position).getFirstposition());
    holder.totalspots.setText(contestsList.get(position).getTotalspots());
    holder.teamsallowed.setText(contestsList.get(position).getTeamsallowed());
    holder.bonususage.setText(contestsList.get(position).getBonususage());
    holder.maintitle.setText(contestsList.get(position).getMaintitle());
    holder.progressBar.setMax(Integer.parseInt(contestsList.get(position).getTotalspots()));
    dr = FirebaseDatabase.getInstance().getReference().child("match1joinedteams").child("contest1");
    dr.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists())
            {
                Long countjoinedteams1 = dataSnapshot.getChildrenCount();
                int a = countjoinedteams1.intValue();
                holder.progressBar.setProgress(a);
            }else{ }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });



}

@Override
public int getItemCount() {
    return contestsList.size();
}

public void setClickListener(ItemClickListener itemClickListener) {
    this.clickListener = itemClickListener;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
    TextView maintitle;
    TextView entry;
    TextView prizepool;
    TextView firstposition;
    TextView teamsallowed;
    TextView bonususage;
    TextView totalspots;
    ProgressBar progressBar;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        maintitle = itemView.findViewById(R.id.maintitle1);
        entry = itemView.findViewById(R.id.entry1);
        prizepool = itemView.findViewById(R.id.prizepool1);
        firstposition = itemView.findViewById(R.id.firstpoisition1);
        teamsallowed = itemView.findViewById(R.id.teamsallowed1);
        bonususage = itemView.findViewById(R.id.bonususage1);
        totalspots = itemView.findViewById(R.id.totalspots1);
        progressBar = itemView.findViewById(R.id.progressbar1);
        itemView.setTag(itemView);
        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (clickListener != null) clickListener.onClick(v, getAdapterPosition());
    }
}

 }


输入活动时AppCrash。
问题出在MainActivity在线上adapterContests.setClickListener(this); Logcat显示尝试在空对象上调用虚拟方法。
当我删除此行时,应用程序工作正常,但单击侦听器将无法正常工作。
之前我仅检索数据应用程序时运行良好,但是在实现onClick应用程序后崩溃

最佳答案

好吧,当尚未初始化适配器时,可以在适配器上调用该方法。

您看到以下行:

adapterContests.setClickListener(this);


将其移动到以下位置:

        ..............
        adapterContests = new AdapterContests(getApplicationContext(),contestsList);
        recyclerView.setAdapter(adapterContests);
        //here...

        adapterContests.notifyDataSetChanged();
        ..........


更新:

//SHOULD BE LIKE THIS
adapterContests.setClickListener(MatchContests.this);

关于java - 在RecyclerView上实现了Click事件,但应用崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61784420/

10-10 08:44
查看更多