【发布时间】:2015-09-22 09:20:38
【问题描述】:
我有一个在我的主类中扩展 AsyncTask 的内部类
public class MainActivity extends Activity {
String variable;
public onCreate(){
onClickListener{
new InnerClass().execute(params);
variable // Here the value is always null
}
}
class InnerClass extends AsyncTask<String,Void,JSONObject>{
protected JSONObject doInBackground(String... params){
/* Relevant Code
*/
}
protected void onPostExecute(JSONObject result){
variable = value; // required value being assigned to the variable
}
}
}
我在内部类中为我的字符串变量“变量”分配了正确的值,但我无法访问我的主类中的值。
【问题讨论】:
-
你不应该公开这样的公共变量。而是使用回调并在内部类中使用它来触发主类中的方法
-
那是因为您正在查看异步任务设置之前的值...
标签: java android oop android-asynctask inner-classes