无法推断ArrayAdapter<>的类型参数 [英] Cannot infer type arguments for ArrayAdapter<>
问题描述
我仍然在处理日历,我几乎已经设法集成了 https://github.com/SundeepK/CompactCalendarView 放入我的片段之一.仅剩一个错误,我做了一些研究,其他人也遇到了问题,例如ArrayList<>.
I am still playing around with my calendar, I already nearly managed to integrate the https://github.com/SundeepK/CompactCalendarView into one of my fragments. There is just one error left, i did some research, others got the problem too for example with ArrayList<>.
示例代码:
final ArrayAdapter adapter = new ArrayAdapter<>
(this,android.R.layout.simple_list_item_1, mutableBookings);
IDE会说:
Error:(87, 38) error: cannot infer type arguments for ArrayAdapter<>
注意:C:... \ Uebersicht.java使用或覆盖不推荐使用的API. 注意:有关详细信息,请使用-Xlint:deprecation重新编译.
Note: C:...\Uebersicht.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
我已经尝试过重新编译,但是结果似乎不起作用
Uebersicht.java:87: error: cannot find symbol
final ArrayAdapter adapter =
new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, mutableBookings);
Uebersicht.java:87: error: cannot find symbol
final ArrayAdapter adapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mutableBookings);
Uebersicht.java:87: error: package android.R does not exist
final ArrayAdapter adapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mutableBookings);
如果有必要,我也可以发布完整的Fragment类,它必须与我的API以及ArrayAdapter正在使用的API有关吗?别忘了我只是一个初学者,我只是想自己做一些事情.
I can post my full Fragment class too if it's necessary, it must be something with my API and the API the ArrayAdapter is using? Don't forget I am just a beginner, I am trying to do just some stuff all by myself.
推荐答案
您需要将Context传递给ArrayAdapter的构造方法.您实际上是在Fragment类中对其进行初始化,因此this
作为上下文无效.尝试致电
You need to pass a Context to the Constructor of ArrayAdapter. You're actually in initializing it in a Fragment class, so this
is not valid as a Context. Try calling
final ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
mutableBookings);
这篇关于无法推断ArrayAdapter<>的类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!