Java 在 OnClickListener 中,我无法访问很多东西 - 如何处理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2076037/
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
Inside OnClickListener I cannot access a lot of things - how to approach?
提问by Ted
Inside an OnClickListener I cannot access most variables "outside" of the scope, like this:
在 OnClickListener 内部,我无法访问范围“外部”的大多数变量,如下所示:
findViewById(R.id.Button01).setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent mainApps = new Intent(Intent.ACTION_MAIN);
mainApps.addCategory(Intent.CATEGORY_LAUNCHER);
List<ActivityInfo> activities = this.getPackageManager().queryIntentActivities(mainApps, 0);
/*
Intent intent = new Intent("com.sygic.drive/com.sygic/drive/.SygicDriveActivity");
startActivity(intent);*/
}
});
in this example I need to get the PacketManager, and I cannot get it since I do not have the Context available inside the OnClickListener.
在此示例中,我需要获取 PacketManager,但我无法获取它,因为我在 OnClickListener 中没有可用的 Context。
I could make a static reference outside, and use it inside, but is that correct? Seems odd to have to do that all the time?
我可以在外面做一个静态引用,然后在里面使用它,但这是正确的吗?必须一直这样做似乎很奇怪?
采纳答案by Lance Nanek
Replace this
in your code with MyActivity.this
where MyActivity is the class name of your Activity subclass.
this
在您的代码中替换为MyActivity.this
MyActivity 是您的 Activity 子类的类名。
Explanation: You are creating an anonymous inner class when you use this part of your code: new OnClickListener() {
Anonymous inner classes have a reference to the instance of the class they are created in. It looks like you are creating it inside an Activity subclass because findViewById is an Activity method. Activity's are a Context, so all you need to do is use the reference to it that you have automatically.
说明:当您使用这部分代码时,您正在创建一个匿名内部类:new OnClickListener() {
匿名内部类具有对创建它们的类的实例的引用。看起来您是在 Activity 子类中创建它,因为 findViewById 是活动方法。Activity 是一个上下文,因此您需要做的就是使用您自动拥有的对它的引用。
回答by broschb
There are a few things you can do, you can create an inner class that implements the onClickListener and pass the necessary arguments into the constructor of the class. I still don't find that the cleanest approach. I usually just create another method to perform my action. So in the onClick(View v) I would do something like this.
您可以做一些事情,您可以创建一个内部类来实现 onClickListener 并将必要的参数传递给类的构造函数。我仍然没有发现最干净的方法。我通常只是创建另一种方法来执行我的操作。所以在 onClick(View v) 我会做这样的事情。
onClick(View v){doMyAction(myParams)}
private void doMyAction(Object params){//do stuff}
And just pass the needed params from the listener method to the method outside the listener.
只需将所需的参数从侦听器方法传递给侦听器外部的方法。
回答by Al.
You could also implement the OnClickListener interface in your class and avoid the need for an anonymous inner class. Then you would set the on click listener like this:
您还可以在您的类中实现 OnClickListener 接口并避免需要匿名内部类。然后你可以像这样设置点击监听器:
findViewById(R.id.Button01).setOnClickListener(this);
findViewById(R.id.Button01).setOnClickListener(this);
If you have multiple buttons using one listener, you can use a switch statement with view.getId() (which corresponds to the view's id in R.id) to distinguish between them.
如果您有多个按钮使用一个侦听器,则可以使用带有 view.getId()(对应于 R.id 中视图的 id)的 switch 语句来区分它们。
回答by WayneSplatter
Change the Intent constructor in use to Intent(context, classname) and use getApplicationContext() in your innerclass. Solved my problem anyway.
将正在使用的 Intent 构造函数更改为 Intent(context, classname) 并在您的内部类中使用 getApplicationContext()。无论如何解决了我的问题。