需要重新启动应用程序以显示添加到 recyclerView 的第一个项目。后来一切正常。
Need to restart the app to show the first item added to recyclerView. Later it all works fine.
[我创建了一个小应用程序,其中只包含我面临的问题。下方视频link
问题:我有一个 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);
}
}
}
经过几个小时的变通,我终于找到了答案。
当应用程序首次启动时,它会创建一个 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
}
瞧!而已。
[我创建了一个小应用程序,其中只包含我面临的问题。下方视频link
问题:我有一个 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);
}
}
}
经过几个小时的变通,我终于找到了答案。
当应用程序首次启动时,它会创建一个 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
}
瞧!而已。