简体   繁体   English

无法引用封闭范围中定义的非最终局部变量jSONString

[英]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.

相关问题 无法引用封闭 scope 中定义的非最终局部变量显示 - Cannot refer to the non-final local variable display defined in an enclosing scope 无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误 - Cannot refer to the non-final local variable button defined in an enclosing scope, Random method error “不能引用封闭 scope 中定义的非最终局部变量。” 更改执行环境后 - “Cannot refer to the non-final local variable defined in an enclosing scope.” after changing Execution Environment 将字符串保存在Login类的sharedpreferences中以在MainActivity中使用-无法引用定义的非最终局部变量编辑器 - Saving a string in sharedpreferences in Login class to be used in MainActivity - Cannot refer to the non-final local variable editor defined Volley:无法在android中引用非最终局部变量 - Volley:Cannot refer to the non-final local variable in android Java-无法引用非最终变量 - Java - Cannot refer to a non-final variable 无法引用非最终变量和计时器 - Cannot refer to non-final variable and Timer 无法引用非最终变量 - Cannot refer to a non-final variable java不能引用非final变量 - java cannot refer to non-final variable 为什么非最终的“局部”变量不能在内部类中使用,而是封闭类的非最终字段可以? - Why a non-final “local” variable cannot be used inside an inner class, and instead a non-final field of the enclosing class can?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM