Java 在setOnClickListener中获取活动(此)
我是java和android开发的新手,我有一个问题,让我解释一下。 我有自定义接口和自定义类,它们将此用作侦听器 在我的HomeActivity中,我对自定义类调用方法,该类通过侦听器(接口)响应,这是一个简短版本:Java 在setOnClickListener中获取活动(此),java,android,Java,Android,我是java和android开发的新手,我有一个问题,让我解释一下。 我有自定义接口和自定义类,它们将此用作侦听器 在我的HomeActivity中,我对自定义类调用方法,该类通过侦听器(接口)响应,这是一个简短版本: public class HomeActivity extends Activity implements WebClientResponseListener { private User user; @Override protected void on
public class HomeActivity extends Activity implements WebClientResponseListener {
private User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
//....
// I call my class and add this as a listener:
MyWebServiceClient mws = new MyWebServiceClient(getApplicationContext());
mws.getProducts(this.user.getToken(), this)
}
//....
@Override
public void onDataDownloadSuccess(JSONObject jsonObject) {
Log.d("DATA", "SUCCESS");
}
}
当我这样运行它时,一切正常。调用了onDataDownloadSuccess方法,我可以看到日志输出
当我尝试从OnClickListener运行它时,会出现问题:
private void sendRequest() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.request_info_title));
builder.setItems(items, this);
//.....
builder.create().show();
}
稍后在同一活动中,我有:
public void onClick(DialogInterface dialogInterface, int which) {
MyWebServiceClient mws = new MyWebServiceClient(getApplicationContext());
mws.getProducts(user.getToken(), this)
}
在这种情况下,不会调用侦听器onDataDownloadSuccess方法。
我可以在控制台中看到:
调用unregisterListener()
及
全部释放1613K,35%释放13400K/20408K,暂停18ms,总计
18毫秒
我在网上寻找解决方案,我已经尝试了一些东西,例如:
//in OnClick
mws.getProducts(user.getToken(), HomeActivity.this);
我创建了私有变量HomeActivity,并在onCreate中调用
homeActivity = this;
后来在onClick我试着通过homeActivity而不是这个
但是没有运气。
提前感谢。由于您需要
上下文
而不是活动
,变量需要为:
Context homeActivity;
或
然后在onCreate()中
在您的活动或家庭活动中使用上下文
或家庭活动
。此
在单击侦听器内部,“
此”
”是单击侦听器的参考。始终尝试使用您的\u活动\u名称。这是因为当您仅使用“此”时,它指向当前的上下文。假设您在OnClickListener
,它是一个匿名类,因此,当您在该类中使用this时。它将指向按钮
而不是活动
。因此,您需要使用activityname指向activity。这。
您可以创建一个上下文
的实例,并在侦听器外部将其初始化为this
,然后在需要的地方调用它上述解决方案应该可以很好地工作,但仍然会添加一些味道。我想这会有助于人们更好地理解
public class SampleActivity extends AppCompatActivity {
private Context sampleActivityContext;
private Button continueButton;
// All your class level variables/fields declared in this section
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sampleActivityContext = this; // Assigning the current activity(i.e.,) SampleActivity context
.......
continueButton = findViewById(R.id.continueButton); // Map the UI element from the XML to the corresponding java element
.......
// Setting the onclick listener for the button
continueButton.setOnClickListener(new View.onClickListener() {
@Override
public void onClick(View view) {
// When you need the context of the activity use sampleActivityContext
// instead of this keyword as this will refer the current view/button within the section
// or you can prefer SampleActivity.this as well
}
}
谁负责调用onDataDownloadSuccess?@blackbelt MyWebServiceClient在完成数据下载后调用onDataDownloadSuccess。onClick是否在内部临时类中声明?@blackbelt否它直接在HomeActivity类中。我试过这样做,但它既不适用于上下文,也不适用于HomeActivity。this@Greg试着通过考试值而不是此值。user.getToken()。看看会发生什么。您确定它到达了click listener内部吗?如果我将上下文添加到getProducts方法,它不允许我,因为:com中的getProducts(java…WebClient响应listener)…ToolstreamWebServiceClient无法应用于(java…,android.content.context)。我试图将其转换到getProducts中的(HomeActivity)上下文,但它不起作用,onDataDownloadSuccess未被调用。请在问题中发布您的活动代码,您收到的错误表示它需要上下文而不是侦听器引用。我的问题中有错误,我将其称为。用户。。。在onCreate方法中,稍后在onClick中,我将用户称为user。。。
context=this;
public class SampleActivity extends AppCompatActivity {
private Context sampleActivityContext;
private Button continueButton;
// All your class level variables/fields declared in this section
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sampleActivityContext = this; // Assigning the current activity(i.e.,) SampleActivity context
.......
continueButton = findViewById(R.id.continueButton); // Map the UI element from the XML to the corresponding java element
.......
// Setting the onclick listener for the button
continueButton.setOnClickListener(new View.onClickListener() {
@Override
public void onClick(View view) {
// When you need the context of the activity use sampleActivityContext
// instead of this keyword as this will refer the current view/button within the section
// or you can prefer SampleActivity.this as well
}
}