[英]The method getActivity() is undefined for the type new Runnable(){}
I am trying to populate a Spinner with a JSON object that I am pulling from a restful API. 我正在尝试使用从宁静的API中提取的JSON对象填充Spinner。
But I cannot figure out how to get Adapter working for the spinner. 但是我不知道如何使Adapter适用于微调器。 I have tried
getActivity()
and I've tried the this
keyword. 我尝试过
getActivity()
并且尝试过this
关键字。 Neither one has worked and I can't find anything on Google where people are trying to update the spinner in this context. 两者均未奏效,我在Google上找不到任何试图在这种情况下更新微调器的东西。
Here is the code I am using. 这是我正在使用的代码。 http://pastebin.com/bQWDHekB .
http://pastebin.com/bQWDHekB 。 When I just
getActivity()
it says, The method getActivity() is undefined for the type new Runnable(){}
当我刚刚说到
getActivity()
时, The method getActivity() is undefined for the type new Runnable(){}
but when I use this
is says The constructor ArrayAdapter<String>(new Runnable(){}, int, String[]) is undefined
但是当我使用
this
的时候说The constructor ArrayAdapter<String>(new Runnable(){}, int, String[]) is undefined
-- EDIT -- As per Maartens' answer, I found that using MainActivity.this
worked better than getApplicationContext()
. -编辑-根据Maartens的回答,我发现使用
MainActivity.this
比getApplicationContext()
更好。 When you use getApplicationContext()
it tends to use a different style and made my spinner have white text on a white background. 当您使用
getApplicationContext()
它倾向于使用不同的样式,并使我的微调器在白色背景上具有白色文本。 Hopefully this helps someone else who had the same troubles as me. 希望这可以帮助遇到其他麻烦的人。
Well, it's because Runnable
doesn't have a getActivity()
method. 好吧,这是因为
Runnable
没有getActivity()
方法。
You can get a reference to the wrapping activity by replacing 您可以通过替换获得包装活动的参考
new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
by 通过
new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, array_spinner);
or, because you just need any Context
或者,因为您只需要任何
Context
new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
which is equivalent to 相当于
new ArrayAdapter<String>(MainActivity.this.getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.