为什么值不相同甚至调用相同的变量

时间:2016-05-25 03:07:20

标签: android arrays string integer

    //global 

    getDatabaseChart();
    addData();


public void addData() {
    int a;
    a = profile.getTotalBelum();
    Log.d("AA",""+a);
    final float[] yData = {a};
    ......
 }

    public void getDatabaseChart(){
    //Creating a string request
    Log.d("MasukTak","MasukTak");
    StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.URL_WEB + "a.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //   hidePDialog();
                    try {

                        Log.d("TgkSini", response);
                        JSONObject json = new JSONObject(response);
                        int success = json.getInt("success");

                        if (success == 1) {
                            user = json.getJSONArray("user");

                            for (int i = 0; i < user.length(); i++) {
                                JSONObject obj = user.getJSONObject(i);

                                profile = new ProfileUser();
                                profile.setTotalBelum(obj.getInt("belum"));


                             Log.d("BelumB",String.valueOf(profile.getTotalBelum()));

                            }
                            abc = profile.getTotalBelum();
                            Log.d("abccc", String.valueOf(abc));


                        } else {
                            Log.d("data2 ", "no user");
                        } ...............

这是我的代码..现在我改变代码..我只是直接整数值..当我看到logcat ..检查log.d abccc ..值为abc = 2 ...然后我检查log.d AA值对于a = 0 ...

如何解决?请帮帮我。

3 个答案:

答案 0 :(得分:1)

如果您尝试将其解析为整数。

解析前检查。或正确处理异常。 例如:

for k,v in dic.items():
    print '\t'.join(k),'\t',v

...     }

答案 1 :(得分:0)

使用此方法处理异常:

public static int parseInt(String number, int defaultValue){
    try{
       return Integer.parseInt(number);
    }catch(NumberFormatException e){
       return defaultValue;
    }
}

这是使用该方法的方法:

a = parseInt(profile.getTotalBelum(), 3);
// if the string cannot be converted to integer,
// the value will be returned to 3 (i.e. the defaultValue)

答案 2 :(得分:0)

检查以下更新方法,

 public void addData() 
 {

    a = convertData(profile.getTotalBelum().toString());
    Log.d("AAAA",""+a);
    aa = convertData(profile.getTotalSedang().toString());
    aaa = convertData(profile.getTotalLulus().toString());

    float[] yData = { a, aa, aaa};
 }

 public int convertData(String strTemp)
 {
    int i = 0;
    try
    {
        i = Integer.parseInt(strTemp);            
    }
    catch(Exception e)
    {
         i = 0;
    }

    return i;
 }