简体   繁体   English

对于新的Runnable(){}类型,未定义方法getActivity()

[英]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.thisgetApplicationContext()更好。 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.

相关问题 对于new Runnable(){}类型,未定义方法findViewById(int) - The method findViewById(int) is undefined for the type new Runnable(){} 对于 Runnable 类型,方法 start() 未定义 - The method start() is undefined for the type Runnable 对于用户数据库,在activity_main中使用的类型未定义方法getactivity() - the method getactivity() is undefined for the type Use In activity_main for User Database 对于ServerWorker类型,方法start()未定义...(java Runnable) - The method start() is undefined for the type ServerWorker… (java Runnable) 在活动中设置Google Analytics(分析)跟踪器时,未为类型活动定义方法getActivity() - when setting a google analytics tacker in an activity, method getActivity() is undefined for the type activity 类型为PendingIntent的getActivity方法不适用于争论 - The method getActivity in the type PendingIntent is not applicable for the arguements 对于类型new Thread(){},未定义方法getSystemService(String)。 - The method getSystemService(String) is undefined for the type new Thread(){} 该类型的方法未定义,这对于Java来说是非常新的 - Method is undefined for the type, very new to java 未为新的FilterQueryProvider类型定义方法fetchDataByName(String) - method fetchDataByName(String) is undefined for type new FilterQueryProvider 对于类型为new ActionListener(){}的方法xy(double,…)未定义 - The method xy(double, …) is undefined for the type new ActionListener(){}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM