[英]Cannot refer to the non-final local variable jSONString defined in an enclosing scope
I am trying to pass JSON string to the server after 60 seconds. 我试图在60秒后将JSON字符串传递给服务器。 At the moment I am facing problem when I try to execute MyAsyncTask from an thread; 目前,当我尝试从线程执行MyAsyncTask时遇到问题。 jSONString
is not accessable at this line new MyAsyncTask().execute(jSONString);
在此行无法访问jSONString
new MyAsyncTask().execute(jSONString);
and I am getting this error Cannot refer to the non-final local variable jSONString defined in an enclosing scope
并且我收到此错误Cannot refer to the non-final local variable jSONString defined in an enclosing scope
This part of code is being called from the onLocationChanged method in the inner class of the MainActivity : 这部分代码是从MainActivity的内部类中的onLocationChanged方法调用的 :
String jSONString = convertToJSON(pLong, pLat, formatted);
PostData sender = new PostData();
sender.timer(jSONString);
PostData class: PostData类:
public class PostData {
String jSONString;
Handler handler = new Handler();
public PostData() {
super();
}
public String getjSONString() {
return jSONString;
}
public void setjSONString(String jSONString) {
this.jSONString = jSONString;
}
public void timer(String jSONString) {
this.jSONString = jSONString;
new Thread(new Runnable() {
@Override
public void run() {
boolean run = true;
while (run) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute(jSONString); //How can I access this variable'jSONString' here?
}
}, 5000);
}
}
}).start();
}
class MyAsyncTask extends AsyncTask<String, Integer, Void> {
@Override
protected Void doInBackground(String... params) {
System.out.println("The output of : doInBackground " +params[0]);
//the connection code.
return null;
}
}
}
使用在jSONString
中声明的PostData
当前尝试访问不是final
timer
方法参数:
new MyAsyncTask().execute(PostData.this.jSONString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.