需要重新启动应用程序来显示添加到recyclerView中的第一个项目.后来一切都好了



[我创建了一个小应用程序,其中包括我所面临的问题。][视频链接]

问题:我有一个recyclerView和一个按钮,将项目添加到recyclerView。在应用程序的第一次启动recyclerView是空的。如果我点击按钮一次,两次,三次,什么也没发生。现在,如果我关闭应用程序并重新启动它,我可以看到这些项目添加到recyclerView。现在,如果我再次点击按钮,项目将按预期添加到recyclerView。这不是那么简单,所以我添加了一个视频来展示确切的问题。

当我添加第一项时,我在data.add(0,i);处获得nullpointerexception。为了防止这种情况,我把它放在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);
    }
}
}

最后经过几个小时的工作,我找到了答案。当应用程序第一次启动时,它正在创建一个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
    }

瞧!这是它。

相关内容

最新更新