我想在android中进行改进,所以我创建了一个待办事项列表。我工作得很好,但是当我想清除手机中的所有应用程序以减少手机中的内存,而我又回到我的应用程序时,它不起作用,并且出现此错误
试图在空对象引用上调用接口方法'int java.util.List.size()'
当我检查logcat时。
List.java
public class List extends AppCompatActivity implements ItemDialog.ItemDialogListener, AdapterView.OnItemClickListener {
private ListView listView ;
private Button addBtn ;
private ArrayList<EltList> eltList ;
private ArrayAdapter<EltList> adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
addBtn = findViewById(R.id.addTask);
listView = findViewById(R.id.listView);
eltList = FileHelper.readData(this);
adapter = new TextAdapter(this,eltList);
listView.setAdapter(adapter);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
}
});
listView.setOnItemClickListener(this);
}
public void openDialog(){
ItemDialog itemDialog = new ItemDialog();
itemDialog.show(getSupportFragmentManager(),"example dialog");
}
@Override
public void applyText(String newTask, String newDateTimeTask) {
EltList elt = new EltList(newTask,newDateTimeTask);
adapter.add(elt);
FileHelper.writeData(eltList,List.this);
Toast.makeText(List.this,"Item added",Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
eltList.remove(position);
adapter.notifyDataSetChanged();
FileHelper.writeData(eltList,this);
Toast.makeText(this,"Delete",Toast.LENGTH_SHORT).show();
}
}
TextAdapter.java
public class TextAdapter extends ArrayAdapter<EltList> {
public TextAdapter(Context context, java.util.List<EltList> eltList){
super(context,0,eltList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item,parent,false);
}
ItemList itemList = (ItemList) convertView.getTag();
if(itemList == null){
itemList = new ItemList();
itemList.taskName = convertView.findViewById(R.id.task);
itemList.taskDate = convertView.findViewById(R.id.task_date) ;
convertView.setTag(itemList);
}
EltList eltList = getItem(position);
itemList.taskName.setText(eltList.getTaskName());
itemList.taskDate.setText(eltList.getTaskDate());
return convertView ;
}
private class ItemList{
public TextView taskName ;
public TextView taskDate ;
}
}
FileHelper
public class FileHelper {
public static final String FILENAME = "file.dat";
public static void writeData(ArrayList<EltList> items, Context context){
try{
FileOutputStream fos = context.openFileOutput(FILENAME,Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(items);
oos.close();
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
}
public static ArrayList<EltList> readData(Context context){
ArrayList<EltList> itemList = null ;
try{
FileInputStream fis = context.openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
itemList = (ArrayList<EltList>) ois.readObject();
}
catch (FileNotFoundException e){
e.printStackTrace();
itemList = new ArrayList<>();
}
catch (IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return itemList ;
}
}
有人有主意吗?
更新 这不是重复的,我已经尝试做一些事情,因为我发现如果我替换了:
ArrayList<EltList> itemList = null ;
通过
ArrayList<EltList> itemList = new ArrayList<>() ;
它可以正常工作,但是如果执行该操作,我将无法继续工作。
即使我尝试使用NullPointerException
捕获错误,也无法正常工作。