2017-10-08 36 views
-4

我可能对我的对象有问题,但我还没弄明白。

这里的问题是:每次我想将文本textViewHoTen设置为nguoiDung.getAnything()时,我的应用程序将停止工作。我可以用另一个字符串来设置,但不能用nguoiDung的字符串来设置。

我在尝试很多事情,我的方式在网上找到,但没有工作。

这是我的应用程序的一部分。希望有人能让我出去。谢谢。


1.类NguoiDung
Android:我无法从我的对象获取字符串

public class NguoiDung { 
    public String username; 
    public String email; 
    public String password; 
    public String ho; 
    public String ten; 
    public String sodienthoai; 

    //Constructor 
    public NguoiDung(String username, String email, String password, String ho, String ten, String sodienthoai) { 
     this.username = username; 
     this.email = email; 
     this.password = password; 
     this.ho = ho; 
     this.ten = ten; 
     this.sodienthoai = sodienthoai; 
    } 


    //Getter Setter 
    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 

    public String getHo() { 
     return ho; 
    } 

    public void setHo(String ho) { 
     this.ho = ho; 
    } 

    public String getTen() { 
     return ten; 
    } 

    public void setTen(String ten) { 
     this.ten = ten; 
    } 

    public String getSodienthoai() { 
     return sodienthoai; 
    } 

    public void setSodienthoai(String sodienthoai) { 
     this.sodienthoai = sodienthoai; 
    } 
} 


2.我的片段通常具有

NguoiDung nguoiDung; 
textViewHoTen = (TextView) V.findViewById(R.id.textViewHoTen); 
@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     inflater = getActivity().getLayoutInflater(); 
     View V = inflater.inflate(R.layout.tab_tai_khoan_view, container, false); 
     getData(V, url); 
     textViewHoTen = (TextView) V.findViewById(R.id.textViewHoTen); 
     textViewHoTen.setText(nguoiDung.getUsername()); 
     return V; 
} 


3.getData()

private void getData(View V, String url){ 
    RequestQueue requestQueue = Volley.newRequestQueue(V.getContext()); 
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, url, null, 
      new Response.Listener<JSONArray>() { 
       @Override 
       public void onResponse(JSONArray response) { 
        for (int i=0; i<response.length(); i++){ 
         try { 
          JSONObject object = response.getJSONObject(i); 
          nguoiDung = new NguoiDung(
            object.getString("Username"), 
            object.getString("Email"), 
            object.getString("Password"), 
            object.getString("Ho"), 
            object.getString("Ten"), 
            object.getString("SoDienThoai") 
          ); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 

       } 
      } 
    ); 
    requestQueue.add(jsonArrayRequest); 
} 
+0

'JsonArrayRequest'是_asynchronous_。 ''setText()'调用''nguoiDung.getUsername()'时,'nguoiDung'不会被设置。 –

+0

[可惜MyApp已停止。我怎么解决这个问题?](https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this) – Zoe

回答

1

发生这种情况,因为在代码中的逻辑错误。钍代码如下

NguoiDung nguoiDung; 

您还没有initialzing它。您正在Web服务响应中初始化此引用变量,但在Web服务响应返回之前,您正试图从nguoiDung变量访问数据。在这个阶段,这是空的。很快你可以做一件事,你可以在得到回应后在文本视图中设置值

+0

好吧,我明白了,我现在要解决它,谢谢 –

+0

我很高兴可以帮助你...如果这有助于你,请不要忘记接受并upvote我的回答:) –

相关问题