gpt4 book ai didi

java - 尝试在空对象引用上调用虚拟方法“boolean java.util.ArrayList.add”

转载 作者:行者123 更新时间:2023-12-02 01:30:11 24 4
gpt4 key购买 nike

我正在创建一个 Android 应用程序。问题是,当我按下按钮并将数据传递到新数组时,它会变成黑色并给出以下错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add (java.lang.Object) 'on a null object reference

通过调试,它在字符串中给出了一些内容:

mExampleList.add(Ticket)

我的ExampleAdapter类:

public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder>{

private ArrayList<ExampleItemRecyclerview>mExampleList;

public class ExampleViewHolder extends RecyclerView.ViewHolder{

public TextView mTextticket;
public TextView mTextredattore;
public TextView mTexttarga;
public TextView mTextdataA;
public TextView mTextmanutenzioneG;
public TextView mTextdataC;

public ExampleViewHolder(View itemView) {
super(itemView);
mTextticket = itemView.findViewById(R.id.txt_ticket);
mTextredattore = itemView.findViewById(R.id.txt_redattore);
mTexttarga = itemView.findViewById(R.id.txt_targa);
mTextdataA = itemView.findViewById(R.id.txt_dataA);
mTextmanutenzioneG = itemView.findViewById(R.id.txt_materiale);
mTextdataC = itemView.findViewById(R.id.txt_dataC);

}
}

public ExampleAdapter(ArrayList<ExampleItemRecyclerview>examplelist){
mExampleList = examplelist;
//todo creazione di una nuova ArrayList Item
//mExampleListFull = new ArrayList<>(examplelist);

}

@Override
public ExampleViewHolder onCreateViewHolder( ViewGroup parent , int view) {
//todo creazione variabile View
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item_recyclerview,parent,false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}

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

ExampleItemRecyclerview currentItem = mExampleList.get(position);

holder.mTextticket.setText(Integer.toString(currentItem.getTicket()));
holder.mTextredattore.setText(currentItem.getRedattore());
holder.mTexttarga.setText(currentItem.getTarga());
holder.mTextdataA.setText(Integer.toString(currentItem.getDataApertura()));
holder.mTextmanutenzioneG.setText(currentItem.getMaterialeGuasto());
holder.mTextdataC.setText(Integer.toString(currentItem.getDataChiusura()));

}

@Override
public int getItemCount() {

return mExampleList.size();
}

}

我的 Visualizza 类(class):

public class Visualizza extends AppCompatActivity {

ArrayList<ExampleItemRecyclerview> mExampleList;

RecyclerView mRecyclearView;
RecyclerView.Adapter mAdapter;
RecyclerView.LayoutManager mLayoutManager;

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

ArrayList<ExampleItemRecyclerview>examplelist = new ArrayList<>();
//examplelist = new ArrayList<>();
examplelist.add(new ExampleItemRecyclerview(123356,"ALESSANDRO","EP562WS",12052018,"Lampeggiante Guasto",23102018));
examplelist.add(new ExampleItemRecyclerview(34567,"FRANCESCO", "EP762NS",19052019,"Motore Guasto",21052019));
examplelist.add(new ExampleItemRecyclerview(34353,"ALESSANDRO","EP760WR",25052017,"Freni Guasti",10122018));


mRecyclearView = findViewById(R.id.miorecyclerView);
mRecyclearView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
//mAdapter = new ExampleAdapter(mExampleList);
mAdapter = new ExampleAdapter(examplelist);
mRecyclearView.setLayoutManager(mLayoutManager);
mRecyclearView.setAdapter(mAdapter);

Bundle incomingMessages = getIntent().getExtras();
if(incomingMessages != null){
int ticket = Integer.parseInt(incomingMessages.getString("ticket"));

String redattore = incomingMessages.getString("redattore");
String targa = incomingMessages.getString("targa");
int dataA = Integer.parseInt(incomingMessages.getString("dataA"));
String materiale = incomingMessages.getString("materiale");
int dataC = Integer.parseInt(incomingMessages.getString("dataC"));

//TODO creazione nuova scheda Ticket
ExampleItemRecyclerview Ticket = new ExampleItemRecyclerview(ticket,redattore,targa,dataA,materiale,dataC);
mExampleList.add(Ticket);
mAdapter.notifyDataSetChanged();

}
}
}

最佳答案

请初始化您arraylist<>在使用它们之前

    private ArrayList<ExampleItemRecyclerview> mExampleList = new ArrayList<>();

关于java - 尝试在空对象引用上调用虚拟方法“boolean java.util.ArrayList.add”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212297/

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