【问题标题】:Why value not same even call same variable [duplicate]为什么值不一样甚至调用相同的变量[重复]
【发布时间】:2016-09-22 11:11:45
【问题描述】:
    //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 ...

如何解决?请帮帮我。

【问题讨论】:

  • 我刚刚编译了你的代码,它工作正常。你能检查出错误发生在哪一行吗?
  • 不要只记录a,记录所有并发布完整的stracktrace!
  • @NecipAllef ...是的,它工作正常,但我得到的值不一样.. huhu

标签: android arrays string integer


【解决方案1】:

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

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

try{
   int a = Integer.parseInt(profile.getTotalBelum());
}catch(NumberFormatException ex){ // handle your exception

... }

【讨论】:

  • @damini mehra 先生 .. 我更新了我的问题 .. 实际上,我的值仍然不同 .. 所以我直接调用整数 .. 但仍然没有得到相同的值 .. 你能看到我的更新吗问题..
【解决方案2】:

使用此方法处理异常:

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)

【讨论】:

  • Sir @Anggrayudi H.. 我更新了我的问题.. 实际上,我仍然没有相同的值.. 所以我直接调用整数.. 但仍然没有得到相同的值.. 你能看到我的更新问题..
【解决方案3】:

检查下面更新的方法,

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

【讨论】:

  • Sir @Vickyexpert .. 我更新了我的问题 .. 实际上,我仍然没有相同的值 .. 所以我直接调用整数 .. 但仍然没有得到相同的值 .. 你能看到我更新的问题..
猜你喜欢
  • 1970-01-01
  • 2019-01-27
  • 2015-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2021-05-18
相关资源
最近更新 更多