简体   繁体   English

尝试从方法检索数据时,变量返回null

[英]Variable returns null when trying to retrieve data from a method

String name;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getData();
    this.setText(name);
}
private void getData(){
    name = object.getString("name");
}

I'm just making my line of code short, anyway the idea is already there. 我只是在缩短我的代码行,无论如何,这个想法已经存在。 So on the method getData() I'm retrieving data from the web. 因此,在方法getData()我正在从Web检索数据。 I am able to test and was able to get the result however when I put the name on the onCreate() it gives me a null value. 我能够测试并能够得到结果,但是当我将名称放在onCreate()它为我提供了一个空值。 How to do some work arounds on this? 如何对此进行一些解决?

public class Testing extends NavigationLiveo{
String name;

@Override
public void onInt(Bundle savedInstanceState) {
    getData();
    Log.wtf("name0", name);
    this.username.setText(name);
}
private void getData() {
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
    StringRequest sr = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String s) {
            try {
                name = object.getString("name");
                Log.wtf("name1", name);
            } catch (Exception e) {
                Log.wtf("testing1", e.getMessage());
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {

        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("user", user_pid);
            return params;
        }
    };queue.add(sr);
}
}

Once again, I've made my coding simple. 再一次,我使编码变得简单。 I can really get the data from the web server. 我真的可以从Web服务器获取数据。 It's just that whenever the I'll try to access the name on the onInt(Bundle savedInstanceState) it returns a null value. 只是每当我尝试访问onInt(Bundle savedInstanceState)上的名称时,它都会返回一个空值。 The log says it all as well. 日志也说明了一切。 "name1" returns a value while "name0" returns null. “ name1”返回一个值,而“ name0”返回空。

Can you put the getData() code? 您可以放入getData()代码吗?
I think that since the method uses AsyncTask or so to download the data from URL asynchronously. 我认为,由于该方法使用AsyncTask左右,从而从URL异步下载数据。 That means the method hasn't completed downloading, the setText(name) code already executed. 这意味着该方法尚未完成下载,而setText(name)代码已执行。 So it displays null . 因此它显示为null

The only explanation is that the object.getString("name") returns null, or you are setting it to null somewhere afterwards. 唯一的解释是object.getString("name")返回null,或者您随后将其设置为null。 By the way, when retreiving data from the web, you should use AsyncTask, for more details refer here . 顺便说一句,当从Web检索数据时,应该使用AsyncTask,有关更多详细信息, 请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM