如果条件给出错误输出

时间:2016-10-13 12:19:22

标签: java android

运行此代码时,它始终通过if块。 如果count不是0,则无关紧要,它始终执行此块。

if (!count.equals("0")) {
    Toast.makeText(getActivity(),"No data in there.Count = "+count, Toast.LENGTH_LONG).show();
    return;
}else{
    Toast.makeText(getActivity(),"Count = "+count, Toast.LENGTH_LONG).show();
    Fragment fragment = new PGSubCatFragment();
    Bundle bCatID = new Bundle();
    bCatID.putString("catid", cID);
    fragment.setArguments(bCatID);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit();
}

你能解释一下是什么问题吗?

以下是我的应用程序的控制台输出:

D/PGCatFragment: Count = 0 gallery
I/gralloc.sc8830: gralloc_register_buffer, handle:0x579da7c0, size:0x40200, fd:64
I/gralloc.sc8830: gralloc_register_buffer, handle:0x574508c0, size:0xcc30, fd:69
I/gralloc.sc8830: gralloc_register_buffer, handle:0x549341d8, size:0x40200, fd:67
I/gralloc.sc8830: gralloc_register_buffer, handle:0x579dc708, size:0x40200, fd:80
I/gralloc.sc8830: gralloc_register_buffer, handle:0x579ea370, size:0x40200, fd:82
D/PGSubCatFragment: {"message":"No gallery found","success":0}
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x579da7c0, size:0x40200, fd:64
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x549341d8, size:0x40200, fd:67
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x579dc708, size:0x40200, fd:80
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x579ea370, size:0x40200, fd:82
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x574508c0, size:0xcc30, fd:69
I/gralloc.sc8830: gralloc_register_buffer, handle:0x579ffea0, size:0x40200, fd:62
I/gralloc.sc8830: gralloc_register_buffer, handle:0x57a002d8, size:0x40200, fd:73
I/gralloc.sc8830: gralloc_register_buffer, handle:0x5225ce28, size:0x40200, fd:70
I/gralloc.sc8830: gralloc_register_buffer, handle:0x57a00a60, size:0x40200, fd:72
D/PGCatFragment: {"success":1,"imgCat":[{"catimg":"http:\/\/live.jmpcbuiltline.com\/admin\/upload\/gallery\/430912.JPG","catid":"1","subcatcount":"5","catname":"Event"},{"catimg":"","catid":"2","subcatcount":"0","catname":"Media"}]}
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x579ffea0, size:0x40200, fd:62
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x57a002d8, size:0x40200, fd:73
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x5225ce28, size:0x40200, fd:70
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x57a00a60, size:0x40200, fd:72
D/PGCatFragment: Count = 5 gallery
I/gralloc.sc8830: gralloc_register_buffer, handle:0x579f5710, size:0x40200, fd:64
I/gralloc.sc8830: gralloc_register_buffer, handle:0x57b4dc88, size:0xcc30, fd:68
I/gralloc.sc8830: gralloc_register_buffer, handle:0x50685a30, size:0x40200, fd:73
I/gralloc.sc8830: gralloc_register_buffer, handle:0x54f8bee8, size:0x40200, fd:80
I/gralloc.sc8830: gralloc_register_buffer, handle:0x579f55f8, size:0x40200, fd:82
D/PGSubCatFragment: {"success":1,"imgSubCat":[{"gname":"1st Event","gid":"1","catimgcount":"11","subcatimg":"http:\/\/live.jmpcbuiltline.com\/admin\/upload\/gallery\/237156.png"},{"gname":"2nd Event","gid":"2","catimgcount":"14","subcatimg":"http:\/\/live.jmpcbuiltline.com\/admin\/upload\/gallery\/4425910.JPG"},{"gname":"3rd Event","gid":"3","catimgcount":"11","subcatimg":"http:\/\/live.jmpcbuiltline.com\/admin\/upload\/gallery\/731556.JPG"},{"gname":"4th Event","gid":"4","catimgcount":"13","subcatimg":"http:\/\/live.jmpcbuiltline.com\/admin\/upload\/gallery\/967434.JPG"},{"gname":"Organise by JMPC","gid":"5","catimgcount":"15","subcatimg":"http:\/\/live.jmpcbuiltline.com\/admin\/upload\/gallery\/5253910.JPG"}]}
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x579f5710, size:0x40200, fd:64
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x50685a30, size:0x40200, fd:73
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x54f8bee8, size:0x40200, fd:80
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x579f55f8, size:0x40200, fd:82
D/dalvikvm: GC_FOR_ALLOC freed 6613K, 24% free 22864K/29756K, paused 51ms, total 51ms
I/gralloc.sc8830: gralloc_unregister_buffer, handle:0x57b4dc88, size:0xcc30, fd:68

2 个答案:

答案 0 :(得分:0)

如果count是字符串

然后使用此if(!count.equalsIgnoreCase("0"))代替if(!count.equals("0"))

如果count为int

然后使用此if(count != 0))代替if(!count.equals("0"))

答案 1 :(得分:0)

根据您的日志判断:

Count = 0 gallery
Count = 5 gallery 

count不是"0",而是"0 gallery

解决方案可能是:

  • 在添加" gallery"部分
  • 之前获取值
  • 使用if(!count.startWith("0 ")){而不省略0之后的空格,以防有像05,06那样的数字。但是这不适用于多个0。
  • 使用正则表达式if(!count.matches("0+ gallery")){+表示1或更多0
  • 在空格字符上拆分count并比较第一个返回的字符串(与第一个选项类似)