Why does this
give me an error in the below fragment?
为什么这会在下面的片段中给我一个错误?
This is my code:
这是我的代码:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, spinnerArray);
4 个解决方案
#1
1
Try this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, spinnerArray);
-
getContext()
was added only on API 23 and should work only with few devices (since Android M still does not have a huge market share).getContext()仅在API 23上添加,并且仅适用于少数设备(因为Android M仍然没有巨大的市场份额)。
-
getActivity()
was added on API 11 and it is NOT deprecated.. So, you can use it.在API 11上添加了getActivity()并且不推荐使用它。所以,你可以使用它。
#2
0
Replace this
with this.getContext()
or even simply just getContext()
用this.getContext()替换它,甚至只是getContext()
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, spinnerArray);
The reason being is that the constructor for an ArrayAdapter
object looks like this,
原因是ArrayAdapter对象的构造函数如下所示,
From https://developer.android.com/reference/android/widget/ArrayAdapter.html,
ArrayAdapter(Context context, int resource, T[] objects)
or
ArrayAdapter(Context context, int resource, List<T> objects)
What was probably happening before is that the class that the line of code was in was most likely not of type Context
; therefore, you can't use this
alone as a parameter in the construction of an ArrayAdapter. If getContext()
doesn't work, then I'd suggest trying getActivity()
or getApplicationContext()
as well.
以前可能发生的是代码行所在的类很可能不是Context类型;因此,您不能单独使用它作为构造ArrayAdapter的参数。如果getContext()不起作用,那么我建议尝试使用getActivity()或getApplicationContext()。
#3
0
Use getActivity()
which return the context of the activity that the fragment is associated with.
使用getActivity()返回与片段关联的活动的上下文。
So your code becomes
所以你的代码变成了
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, spinnerArray);`]
#4
-2
You can try getActivity().getApplicationContext()
instead of using just this
in fragment.
你可以尝试使用getActivity()。getApplicationContext()而不是在片段中使用它。
#1
1
Try this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_spinner_item, spinnerArray);
-
getContext()
was added only on API 23 and should work only with few devices (since Android M still does not have a huge market share).getContext()仅在API 23上添加,并且仅适用于少数设备(因为Android M仍然没有巨大的市场份额)。
-
getActivity()
was added on API 11 and it is NOT deprecated.. So, you can use it.在API 11上添加了getActivity()并且不推荐使用它。所以,你可以使用它。
#2
0
Replace this
with this.getContext()
or even simply just getContext()
用this.getContext()替换它,甚至只是getContext()
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_spinner_item, spinnerArray);
The reason being is that the constructor for an ArrayAdapter
object looks like this,
原因是ArrayAdapter对象的构造函数如下所示,
From https://developer.android.com/reference/android/widget/ArrayAdapter.html,
ArrayAdapter(Context context, int resource, T[] objects)
or
ArrayAdapter(Context context, int resource, List<T> objects)
What was probably happening before is that the class that the line of code was in was most likely not of type Context
; therefore, you can't use this
alone as a parameter in the construction of an ArrayAdapter. If getContext()
doesn't work, then I'd suggest trying getActivity()
or getApplicationContext()
as well.
以前可能发生的是代码行所在的类很可能不是Context类型;因此,您不能单独使用它作为构造ArrayAdapter的参数。如果getContext()不起作用,那么我建议尝试使用getActivity()或getApplicationContext()。
#3
0
Use getActivity()
which return the context of the activity that the fragment is associated with.
使用getActivity()返回与片段关联的活动的上下文。
So your code becomes
所以你的代码变成了
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, spinnerArray);`]
#4
-2
You can try getActivity().getApplicationContext()
instead of using just this
in fragment.
你可以尝试使用getActivity()。getApplicationContext()而不是在片段中使用它。