Android:我无法从我的Object中获取字符串

时间:2017-10-08 08:36:18

标签: android

我的对象可能有问题,但我还没弄清楚。

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

我正在尝试很多东西,我在网上发现的方式也没有找到工作。

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


1。 NguoiDung类

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);
}

1 个答案:

答案 0 :(得分:1)

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

NguoiDung nguoiDung;

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