我有在 UI 线程中运行代码的内部类,我需要将变量传递给 run() 方法。
我尝试在没有数组的情况下传递最终 res,但出现错误,表明有必要使用数组 res[0]。 在本例中,我需要初始化 res[],因为它会抛出 NullPointerException。
还有其他方法可以将变量传递到内部类吗?
private String sendRequest(String url, String... data) {
final Connection.Response[] res = {};
...
try {
final Connection connection = Jsoup.connect(url)
.method(Connection.Method.POST)
.cookies(cookies)
.timeout(30000)
.ignoreContentType(true);
if (data != null) {
connection.data(data);
}
((Activity) con).runOnUiThread(new Runnable() {
@Override
public void run() {
try {
res[0] = connection.execute();
} catch (IOException e) {
e.printStackTrace();
}
}
});
result = Jsoup.parse(res[0].parse().outerHtml(), "UTF-8").text();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
最佳答案
res[]
被定义为空数组(没有位置),然后您尝试将某些内容分配给 res[]
中的位置 0,但没有位置0,因为需要给它一个大小...
final Connection.Response[] res = new Connection.Response[requiredArraySize];
在您的情况下,requiredArraySize
可能是 1。
关于java - 变量尚未从内部类访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241520/