引用内部类中的非最终变量数据 [英] Referring to a non-final variable data inside an inner class

查看:193
本文介绍了引用内部类中的非最终变量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GWT做一个程序。这里是我有问题的片段

  private String [] populateRSSData(){
1 ==>> ; String [] data = null;
尝试{
new RequestBuilder(RequestBuilder.GET,
../database.php?action=populaterss\").sendRequest(null,
new RequestCallback(){

@Override
public void onResponseReceived(Request request,
Response response){
== ==>> data = response.getText()。split(〜 );
}

@Override
public void onError(Request request,Throwable exception){
Window.alert(exception.getMessage());
}
});
} catch(RequestException e){
Window.alert(e.getMessage());
}

返回数据;





$ b现在问题出现,我得到一个错误,即变量 1 ==>>> data 应该被声明为 final 。但是,如果我将它声明为 final ,那么我不能将数据存储在 2 ==>>



我得到的错误



不能引用内部类中的非最终变量数据用不同的方法定义RSS_Manager.java



请建议

解决方案

即使您设法克服错误,该函数也不会执行您打算执行的操作。
当您的方法populateRSSData()将立即返回时,您正在创建的回调会异步通知从服务器收到的响应。



您需要重新考虑将您的设计考虑为异步。

编辑:请参阅习惯于异步调用



编辑:
上述链接引用


要理解的重要问题是,在调用RPC调用后执行的代码将在实际往返服务器的过程中执行。虽然onSuccess()方法中的代码是与调用内联定义的,但直到调用代码返回到JavaScript主循环,并且服务器的结果消息返回时才会执行。



Im doing a program in GWT. Here is the snippet where Im having problem

 private String[] populateRSSData() {
  1==>> String[] data = null;
  try {
   new RequestBuilder(RequestBuilder.GET,
     "../database.php?action=populaterss").sendRequest(null,
     new RequestCallback() {

      @Override
      public void onResponseReceived(Request request,
        Response response) {
       2==>> data=response.getText().split("~");
      }

      @Override
      public void onError(Request request, Throwable exception) {
       Window.alert(exception.getMessage());
      }
     });
  } catch (RequestException e) {
   Window.alert(e.getMessage());
  }

  return data;
 }

Now the problem arises that I get an error that the variable 1==>> data should be declared final. But if I declare it as final then i cannot store the datas in 2==>>

The error i get

Cannot refer to a non-final variable data inside an inner class defined in a different method RSS_Manager.java

Please suggest

解决方案

Even if you manage to get over the error, the function will not do what you intend it to do. The callback you are creating will be notified asynchronously of the response received from server while your method populateRSSData() will return immediately.

You need to rethink your design taking the asynchrony into account.

EDIT: see Getting Used to Asynchronous Calls

EDIT: Quote from the above link

The important issue to understand is that that the code that follows the RPC call invocation will be executed while the actual round trip to the server is still in progress. Although the code inside the onSuccess() method is defined inline with the call, it will not be executed until both the calling code returns back to the JavaScript main loop, and the result message from the server returns.

这篇关于引用内部类中的非最终变量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆