列表视图问题 "cannot resolve constructor arrayadapter anonymous"

ListView problem "cannot resolve constructor arrayadapter anonymous"

我在这一行遇到错误:

arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, allOnDay);

错误是:

cannot resolve constructor arrayadapter anonymous...

我在 onCreate:

中有这段代码
ArrayList<String> allOnDay = new ArrayList<>();
cal_ListView = findViewById(R.id.cal_ListView);
for (int i = 0; i < data.size(); i++) {
   if (values.get(i).equals(year+"")) {
      if (keys.get(i).getMonth()== month +1 && keys.get(i).getDay() == dayOfMonth) {
         allOnDay.add(data.get(i));
      }
   }
}
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, allOnDay);
cal_ListView.setAdapter(arrayAdapter);

关于如何纠正这个问题的任何建议?

可能您尝试初始化 ArrayAdapter 而不是 Activity context。在那种情况下使用 getApplicationContext() or getContext() or getActivity() 而不是 this.

使用

arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, allOnDay);

而不是

arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, allOnDay);