[我创建了一个小应用程序,其中包含我面临的问题。视频链接]
问题:我有一个recyclerView
和一个向recyclerView
添加项目的按钮。在应用首次启动时,recyclerView
为空。如果我按下按钮一次,两次,三次,没有任何反应。现在,如果我关闭应用程序并重新启动它,我可以看到这些项目已添加到recyclerView。现在,如果我再次点按该按钮,则会按预期将项目添加到recyclerView
。这不是那么简单,所以我添加了一个video来显示确切的问题。
当我添加第一个项目时,我在data.add(0,i);
获取nullPointerExeption。为了防止我将它放在try-catch中。
这是一些代码: 的 MainActivity.java
public class MainActivity extends ActionBarActivity {
private RecyclerView recyclerView;
private InfoTodayAdapter adapter;
FileOutputStream fos;
FileInputStream fis;
List<InformationToday> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview_today);
try {
adapter = new InfoTodayAdapter(this,getData());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public List<InformationToday> getData() throws IOException, ClassNotFoundException {
fis = openFileInput("arrayListToday");
ObjectInputStream ois = new ObjectInputStream(fis);
data = (List<InformationToday>) ois.readObject();
ois.close();
fis.close();
return data;
}
public void addWater(View view) throws IOException {
InformationToday i = new InformationToday();
i.volume = "100";
try { // I get a nullPointerException if I dont include this try.
data.add(0,i);
adapter.notifyItemInserted(0);
}catch (Exception e){
}
fos = openFileOutput("arrayListToday", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(data);
oos.close();
fos.close();
}
}
InfoTodayAdapter.java (我猜不需要)
public class InfoTodayAdapter extends RecyclerView.Adapter<InfoTodayAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<InformationToday> data2 = Collections.emptyList();
public InfoTodayAdapter(Context context,List<InformationToday> data){
inflater = LayoutInflater.from(context);
this.data2 = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_row_today, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
InformationToday current = data2.get(position);
String s = current.volume;
holder.volume.setText(s);
}
@Override
public int getItemCount() {
return data2.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView volume;
public MyViewHolder(View itemView) {
super(itemView);
volume = (TextView) itemView.findViewById(R.id.volume);
}
}
}
答案 0 :(得分:0)
最后经过数小时的解决后,我找到了答案。
当应用程序第一次启动时,它创建了一个IOexception,因为data
对象的文件不存在。
我所做的只是,我发现错误的地方,我添加了丢失的东西。如上所述,只做了以下更改:
try {
adapter = new InfoTodayAdapter(this,getData());
} catch (IOException e) {
e.printStackTrace();
data = new ArrayList<>(); // Added this line
adapter = new InfoTodayAdapter(this,data); // Added this line as well
}
瞧!而已。