Java 适配器崩溃,方法getItemCount nullpointer中

Java 适配器崩溃,方法getItemCount nullpointer中,java,android,nullpointerexception,Java,Android,Nullpointerexception,当我在调试模式下运行我的项目时,它不会出现问题,但当我挤进正常的android studio模式时,它会崩溃 我的适配器 public class InfoAdapter extends RecyclerView.Adapter<InfoAdapter.ViewHolder> { Context context; Screen screen; public InfoAdapter(Context context, Screen screen) { this.context

当我在调试模式下运行我的项目时,它不会出现问题,但当我挤进正常的android studio模式时,它会崩溃

我的适配器

public class InfoAdapter extends RecyclerView.Adapter<InfoAdapter.ViewHolder> {
Context context;
Screen screen;

public InfoAdapter(Context context, Screen screen) {
    this.context = context;
    this.screen = screen;
}

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

@Override
public void onBindViewHolder(@NonNull InfoAdapter.ViewHolder holder, int position) {
    holder.name.setText(screen.getInfo().get(position).getName());
    holder.data.setText(screen.getInfo().get(position).getData());

}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView name, data;

    public ViewHolder(View view) {
        super(view);

        name = view.findViewById(R.id.name);
        data = view.findViewById(R.id.data);
    }
}
公共类InfoAdapter扩展了RecyclerView.Adapter{
语境;
屏幕;
公共信息适配器(上下文、屏幕){
this.context=上下文;
this.screen=屏幕;
}
@非空
@凌驾
public InfoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
LayoutFlater充气机=LayoutFlater.from(上下文);
视图=充气机。充气(R.layout.info\u列表,父项,false);
返回新的InfoAdapter.ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull InfoAdapter.ViewHolder,int位置){
holder.name.setText(screen.getInfo().get(position.getName());
holder.data.setText(screen.getInfo().get(position.getData());
}
@凌驾
public int getItemCount(){
返回屏幕.getInfo().size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder{
文本视图名称、数据;
公共视图持有者(视图){
超级(视图);
name=view.findviewbyd(R.id.name);
数据=view.findviewbyd(R.id.data);
}
}
}

我的片段

public class InvestmentFragment extends Fragment implements FoundView{

TextView titleInvestment, fundNameInvestment, whatIs, definition, riskTitle, infoTitle, fundMonth,
        cdiMonth, fundYear, cdiYear, fundTwelveMonths, cdiTwelveMonths;
View view;

RecyclerView recyclerViewInfo;
RecyclerView recyclerViewDownInfo;
public Screen screen = new Screen();
InfoAdapter adapter;
DownInfoAdapter downInfoAdapter;
public List<DownInfo> downInfos = new ArrayList<>();

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_investment, container, false);

    onResume();

    //Recycler info items
    recyclerViewInfo = view.findViewById(R.id.recyclerviewInfo);
    recyclerViewInfo.setHasFixedSize(true);
    final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerViewInfo.setNestedScrollingEnabled(false);
    recyclerViewInfo.setLayoutManager(layoutManager);
    adapter = new InfoAdapter(getContext(), screen);
    recyclerViewInfo.setAdapter(adapter);

    //RecyclerView DownInfo items
    recyclerViewDownInfo = view.findViewById(R.id.recyclerviewDowninfo);
    recyclerViewDownInfo.setHasFixedSize(true);
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerViewDownInfo.setNestedScrollingEnabled(false);
    recyclerViewDownInfo.setLayoutManager(linearLayoutManager);
    downInfoAdapter = new DownInfoAdapter(getContext(), screen);
    recyclerViewDownInfo.setAdapter(downInfoAdapter);

    TextView textInvestment = view.findViewById(R.id.text_investment);
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "DINEngschriftStd.otf");
    textInvestment.setTypeface(font);

    titleInvestment = view.findViewById(R.id.title);
    fundNameInvestment = view.findViewById(R.id.fundName);
    whatIs = view.findViewById(R.id.whatIs);
    definition = view.findViewById(R.id.definition);
    riskTitle = view.findViewById(R.id.riskTitle);
    infoTitle = view.findViewById(R.id.infoTitle);
    fundMonth = view.findViewById(R.id.fundMonth);
    cdiMonth = view.findViewById(R.id.cdiMonth);
    fundYear = view.findViewById(R.id.fundYear);
    cdiYear = view.findViewById(R.id.cdiYear);
    fundTwelveMonths = view.findViewById(R.id.fundTwelveMonths);
    cdiTwelveMonths = view.findViewById(R.id.cdiTwelveMonths);



    return view;

}


@Override
public void onResume() {
    super.onResume();
    FoundPresenter foundPresenter = new FoundPresenter(this);
    foundPresenter.getFounds();
}

@Override
public void founds(Screen screen) {
    adapter = new InfoAdapter(getContext(), screen);
    downInfoAdapter = new DownInfoAdapter(getContext(), screen);
    recyclerViewInfo.setAdapter(adapter);
    recyclerViewDownInfo.setAdapter(downInfoAdapter);
    titleInvestment.setText(screen.getTitle());
    fundNameInvestment.setText(screen.getFundName());
    whatIs.setText(screen.getWhatIs());
    definition.setText(screen.getDefinition());
    riskTitle.setText(screen.getRiskTitle());
    infoTitle.setText(screen.getInfoTitle());
    fundMonth.setText(screen.getMoreInfo().getMonth().getFund()+"%");
    cdiMonth.setText(screen.getMoreInfo().getMonth().getFund()+"%");
    fundYear.setText(screen.getMoreInfo().getYearMoreInfo().getFund()+"%");
    cdiYear.setText(screen.getMoreInfo().getYearMoreInfo().getcDI()+"%");
    fundTwelveMonths.setText(screen.getMoreInfo().get_12months().getFund()+"%");
    cdiTwelveMonths.setText(screen.getMoreInfo().get_12months().getcDI()+"%");
}
公共类InvestmentFragment扩展了FoundView{
text查看标题投资、基金名称投资、whatIs、定义、风险标题、信息标题、基金月、,
cdiMonth、基金年、cdiYear、基金十二个月、Cditwelvemonth;
视图;
RecyclerView recyclerViewInfo;
RecyclerView recyclerViewDownInfo;
公共屏幕=新屏幕();
信息适配器;
下行信息适配器下行信息适配器;
public List downfos=new ArrayList();
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
视图=充气机。充气(R.layout.fragment\u投资,容器,假);
onResume();
//回收商信息项目
recyclerViewInfo=view.findViewById(R.id.recyclerViewInfo);
RecycleServiceInfo.setHasFixedSize(true);
最终LinearLayoutManager布局管理器=新的LinearLayoutManager(getContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
RecycleServiceInfo.setNestedScrollingEnabled(false);
RecycleServiceInfo.setLayoutManager(layoutManager);
adapter=newinfoadapter(getContext(),screen);
RecycleServiceInfo.setAdapter(适配器);
//RecyclerView DownInfo项目
recyclerViewDownInfo=view.findViewById(R.id.recyclerViewDownInfo);
RecycleServiceWdownInfo.setHasFixedSize(true);
最终LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(linearLayoutManager.VERTICAL);
RecycleServiceWdownInfo.setNestedScrollingEnabled(false);
recyclerViewDownInfo.setLayoutManager(linearLayoutManager);
downInfoAdapter=新的downInfoAdapter(getContext(),屏幕);
recycleServiceWdownInfo.setAdapter(downInfoAdapter);
TextView textInvestment=view.findViewById(R.id.text\u投资);
Typeface font=Typeface.createFromAsset(getActivity().getAssets(),“DINEngschriftStd.otf”);
textInvestment.setTypeface(字体);
titleInvestment=view.findViewById(R.id.title);
fundNameInvestment=view.findViewById(R.id.fundName);
whatIs=view.findViewById(R.id.whatIs);
定义=view.findViewById(R.id.definition);
riskTitle=view.findViewById(R.id.riskTitle);
infoTitle=view.findviewbyd(R.id.infoTitle);
fundMonth=view.findviewbyd(R.id.fundMonth);
cdiMonth=view.findviewbyd(R.id.cdiMonth);
fundYear=view.findviewbyd(R.id.fundYear);
cdiYear=view.findviewbyd(R.id.cdiYear);
fundTwelveMonths=view.findViewById(R.id.fundTwelveMonths);
cdiTwelveMonths=view.findviewbyd(R.id.cdiTwelveMonths);
返回视图;
}
@凌驾
恢复时公开作废(){
super.onResume();
FoundPresenter FoundPresenter=新的FoundPresenter(此);
foundPresenter.getFounds();
}
@凌驾
公共作废创建(屏幕){
adapter=newinfoadapter(getContext(),screen);
downInfoAdapter=新的downInfoAdapter(getContext(),屏幕);
RecycleServiceInfo.setAdapter(适配器);
recycleServiceWdownInfo.setAdapter(downInfoAdapter);
titleInvestment.setText(screen.getTitle());
fundNameInvestment.setText(screen.getFundName());
whatIs.setText(screen.getWhatIs());
setText(screen.getDefinition());
riskTitle.setText(screen.getRiskTitle());
infoTitle.setText(screen.getInfoTitle());
fundMonth.setText(screen.getMoreInfo().getMonth().getFund()+“%”;
cdiMonth.setText(screen.getMoreInfo().getMonth().getFund()+“%”;
fundYear.setText(screen.getMoreInfo().getYearMoreInfo().getFund()+“%”;
cdiYear.setText(screen.getMoreInfo().getYearMoreInfo().getcDI()+“%”;
fundtweevemonths.setText(screen.getMoreInfo().get_12months().getFund()+“%”;
cdiTwelveMonths.setText(screen.getMoreInfo().get_12months().getcDI()+“%”;
}
}

java.lang.NullPointerException:尝试对空对象引用调用接口方法“int java.util.List.size()
在com.example.allan.santanderapp.adapter.InfoAdapter.getItemCount(InfoAdapter.java:41)上,异常消息非常清楚

可能您没有用信息列表填充
屏幕
对象,这就是为什么
infos
为空,并且您正试图在该空对象上调用方法
size()


处理screen对象时请显示。

screen类来自何处?可能是一个好主意,为那个类发布源代码。我解决了它,谢谢。这正是问题所在,我设法解决了它,谢谢。很高兴听到@Alan。请把答案投上去;)