gpt4 book ai didi

java - MainActivity 不是封闭类

转载 作者:行者123 更新时间:2023-12-02 03:48:23 27 4
gpt4 key购买 nike

如何解决这个问题?

MainActivity.this is not an enclosing class.

谢谢

public class uploadToServer extends AsyncTask<Void, Void, String> {

protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pd= new ProgressDialog(MainActivity.this); //error is here
pd.setMessage("Wait image uploading!");
pd.show();
}
}

最佳答案

AsyncTask的构造函数中,传递Context并在需要时使用它

public class uploadToServer extends AsyncTask<Void, Void, String> {
private Context mContext;
public uploadToServer (Context context){
mContext = context;
}

protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pd= new ProgressDialog(mContext); //change is here
pd.setMessage("Wait image uploading!");
pd.show();
}
}

像这样从MainActivity调用,

uploadToServer task = new uploadToServer(getApplicationContext());
task.execute();

关于java - MainActivity 不是封闭类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36118131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com