如何从另一个类获取对 findViewById 的访问权限?
我们都知道你不能在 findViewById
上使用静态,所以......
基本代码是:
public class DiffViewFlowExample extends Activity implements OnItemClickListener {
@Override
public void onCreate() {
}
static void hereismymethod() {
}
如何在这里使用 findViewById
?我知道我不能在本地,但是因为 static
不起作用...
哦,你可能会说:将其添加为参数,我会的,但我会从服务中调用hereismymethod,我们都知道服务不喜欢玩与显示有关的东西......
那么有人可以救我吗?
We all know you can't use static on findViewById
so ...
The basic code is:
public class DiffViewFlowExample extends Activity implements OnItemClickListener {
@Override
public void onCreate() {
}
static void hereismymethod() {
}
How can I use a findViewById
here? I know I can't locally but because static
won't work then...
Oh you might say: add it as an argument,well I would,but I will call the hereismymethod from a service,and we all know that services don't like to play with stuff that is about display...
So can anybody save me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该从
Activity
外部对Activity
调用 method2,无论是否静态。如果您想从Service
中对您的Activity
进行操作,则创建一个侦听器接口,然后将您的 Activity 注册为服务内部的侦听器。然后,在适当的时候,让您的服务找到视图
并对其进行操作。要允许服务访问Activity
中的视图,只需让您的接口方法传递对Activity
的引用即可。然后,服务可以调用 Activity.findViewById() 并执行其想做的任何操作。You shouldn't be calling method2, static or not, on an
Activity
from outside of theActivity
. If you want to act upon yourActivity
from within theService
, then create a listener interface and then register your activity as a listener inside of the service. Then, when it's appropriate, have your service find theview
and act on it. To allow the service to access the views in yourActivity
, simply make your interface method pass a reference to theActivity
. Then, the Service can call activity.findViewById() and do whatever it wants to it.