Java 无法推断 ArrayAdapter<> 的类型参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34388547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot infer type arguments for ArrayAdapter<>
提问by Patpatolino
I am still playing around with my calendar, I already nearly managed to integrate the https://github.com/SundeepK/CompactCalendarViewinto one of my fragments. There is just one error left, i did some research, others got the problem too for example with ArrayList<>.
我还在玩我的日历,我几乎已经设法将https://github.com/SundeepK/CompactCalendarView集成到我的一个片段中。只剩下一个错误,我做了一些研究,其他人也遇到了这个问题,例如 ArrayList<>。
Example code:
示例代码:
final ArrayAdapter adapter = new ArrayAdapter<>
(this,android.R.layout.simple_list_item_1, mutableBookings);
The IDE says:
IDE 说:
Error:(87, 38) error: cannot infer type arguments for ArrayAdapter<>
Note: C:...\Uebersicht.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details.
注意:C:...\Uebersicht.java 使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。
I already tried to recompile, but the result doesn't seem to work
我已经尝试重新编译,但结果似乎不起作用
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);
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.
如果有必要,我也可以发布我的完整 Fragment 类,它必须与我的 API 和 ArrayAdapter 使用的 API 有关吗?不要忘记我只是一个初学者,我正在尝试自己做一些事情。
采纳答案by Benjamin Scharbau
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
您需要将 Context 传递给 ArrayAdapter 的构造函数。您实际上是在 Fragment 类中对其进行初始化,因此this
作为 Context 无效。试试打电话
final ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1,
mutableBookings);
回答by Kingfisher Phuoc
Try using type in your Adapter
declaring. You are missing the type
of your mutableBookings
:
尝试在Adapter
声明中使用类型。你错过了type
你的mutableBookings
:
final ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mutableBookings);
回答by H.Josue
Sometimes the methods do not run directly in the activity and do not have access to it, not because of this, but because of the getApplicationContext ()
有时方法并没有直接在activity中运行并且无法访问它,不是因为这个,而是因为getApplicationContext()
ArrayAdapter adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, mutableBookings);