gpt4 book ai didi

java - RecyclerView 抛出 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 04:03:07 25 4
gpt4 key购买 nike

我的回收器 View 抛出java.lang.NullPointerException:

Attempt to invoke virtual method 'java.util.ArrayList com.msit.example.bunty.regform.entities.DataStore.getDataStore()' on a null object reference at com.msit.example.bunty.regform.adapters.MyRecyclerAdapter.getItemCount(MyRecyclerAdapter.java:65)

MyRecyclerAdapter.java

package com.msit.example.bunty.regform.adapters;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.R.*;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.msit.example.bunty.regform.R;
import com.msit.example.bunty.regform.entities.DataStore;
import com.msit.example.bunty.regform.entities.UserData;

import org.w3c.dom.Text;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**l
* Created by bunty on 1/5/16.
*/

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>
{
DataStore userDataStore=new DataStore();
//ArrayList<UserData> userDataStore=new ArrayList<UserData>();
public MyRecyclerAdapter(DataStore userDataStore)
{
this.userDataStore=userDataStore;
}

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

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

holder.fNameView.setText(userDataStore.getDataStore().get(position).getFirstName());
holder.lNameView.setText(userDataStore.getDataStore().get(position).getLastName());
holder.addressView.setText(userDataStore.getDataStore().get(position).getAddress());
holder.cityView.setText(userDataStore.getDataStore().get(position).getCity());
final String city=userDataStore.getDataStore().get(position).city;
holder.cityView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(v.getContext(),city,Toast.LENGTH_SHORT).show();
}
});
}

@Override
public int getItemCount() {
if(!userDataStore.getDataStore().isEmpty()&&userDataStore.getDataStore()!=null) {
return userDataStore.getDataStore().size();
}
else
{
return 0;
}
}

public static class ViewHolder extends RecyclerView.ViewHolder {

TextView fNameView;
TextView lNameView;
TextView addressView;
Button cityView;
CardView cardView;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
fNameView = (TextView) itemView.findViewById(R.id.fn_view);
lNameView = (TextView) itemView.findViewById(R.id.ln_view);
addressView = (TextView) itemView.findViewById(R.id.addr_view);
cityView=(Button)itemView.findViewById(R.id.city_view);
cardView = (CardView) itemLayoutView.findViewById(R.id.card_list);
}

}
}

我有两个按钮“保存”和“show_details”,如果最初没有保存任何值,则当我单击“show_details”时应用程序崩溃,并在 getitemcount() 方法中引发异常。

最佳答案

像这样修复 getItemCount 方法

@Override 
public int getItemCount() {
if(userDataStore == null){return 0;}

if(!userDataStore.getDataStore().isEmpty()&&userDataStore.getDataStore()!=null) {
return userDataStore.getDataStore().size();
} else{

return 0;
}
}

关于java - RecyclerView 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34684500/

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