如何从匿名方法获取此活动实例的指针 [英] How to get this pointer to activity instance from anonymous method

查看:60
本文介绍了如何从匿名方法获取此活动实例的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何从Java / Android中的匿名方法中获取指向我的活动对象实例的指针。



问题是'this'指针给了我一个函数实例:



错误:(44,17)错误:类MyTask中的构造函数MyTask无法应用给定类型;

必需:活动

找到:匿名OnCheckedChangeListener

原因:实际参数< anonymous oncheckedchangelistener =>无法通过方法调用转换转换为Activity



< pre lang =java> 
公共类MyActivity扩展AppCompatActivity {

切换mySwitch = null;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

mySwitch =(Switch)findViewById(R.id.myswitch2);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
String val =0;

new MyTask(this).execute(val);< -----这个指针在这里
}
});
}
}





MyTask构造函数

 MyTask(活动活动)
{
caller = activity;
}





我想避免在班上存储对该实例的引用。



谢谢!



我的尝试:



this.class

this.getClass()

getActivity()

getApplicationContext()

解决方案

您可以尝试以下两种方法中的任何一种:

1.在MyActivity中创建一个返回this的方法。 ie

活动getActivity(){

return ;
}



然后从匿名方法调用此方法。

  new  MyTask(getActivity())。execute(val); 



2.或者您可以使用它来访问this 。



OuterClass.this



在你的情况下

  new  MyTask(MyActivity。 this )。execute(val); 


Hi,
how can i get a pointer to my activity object instance from within an anonymous method in Java/Android.

The problem is that the 'this' pointer gives me an instance to the function:

Error:(44, 17) error: constructor MyTask in class MyTask cannot be applied to given types;
required: Activity
found: anonymous OnCheckedChangeListener
reason: actual argument <anonymous oncheckedchangelistener=""> cannot be converted to Activity by method invocation conversion

<pre lang="java">
public class MyActivity extends AppCompatActivity {

    Switch mySwitch = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        mySwitch = (Switch) findViewById(R.id.myswitch2);
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                String val= "0";

                new MyTask(this).execute(val); <----- this pointer here
            }
        });
    }
}



MyTask constructor

MyTask(Activity activity)
{
    caller = activity;
}



I would like to avoid having a stored reference to the instance in my class.

Thanks!

What I have tried:

this.class
this.getClass()
getActivity()
getApplicationContext()

解决方案

You could try any of the two approaches :
1. inside MyActivity create a method which return "this" . i.e

Activity getActivity(){

 return this;
}


and then call this method from anonymous method.

new MyTask(getActivity()).execute(val);


2. or you can use this to access the "this" .

OuterClass.this

In your case

new MyTask(MyActivity.this).execute(val); 


这篇关于如何从匿名方法获取此活动实例的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆