You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中Java的if语句未生效问题求助

问题:第二个数值比较的if语句未生效

从TextView获取文本转换为数值后,第一个判断步数的if语句正常生效,但第二个判断卡路里与目标值大小的if语句完全没有反应。尝试将所有数值转为Integer类型后问题依旧,代码运行无报错。

相关代码

初始化部分

public void getCurrentData() {
    TextView user = (TextView) getView().findViewById(R.id.global_text_username);
    String username = getActivity().getIntent().getStringExtra("email");
    String userId = username.substring(0, 5);
    user.setText(username);

    Drawable unreach = getResources().getDrawable(R.drawable.baseline_remove_circle_24, null);

    TextView user_current_cals = (TextView) getView().findViewById(R.id.home_text_calcode);
    TextView user_current_steps = (TextView) getView().findViewById(R.id.home_text_stepcode);
    TextView user_current_target = (TextView) getView().findViewById(R.id.home_text_targetcode);

    ImageView current_cals = (ImageView) getView().findViewById(R.id.home_img_calstatus);
    ImageView current_target = (ImageView) getView().findViewById(R.id.home_img_targetstatus);
    ImageView current_steps = (ImageView) getView().findViewById(R.id.home_img_stepstatus);

数值判断部分

String ucc = user_current_cals.getText().toString();
    double check_current_cals = Double.parseDouble(ucc);

    String uct = user_current_target.getText().toString();
    double check_current_target = Double.parseDouble(uct);

    String ucs = user_current_steps.getText().toString();
    int check_current_steps = Integer.parseInt(ucs);

    if (check_current_steps < 1000) {
        current_steps.setColorFilter(Color.argb(255, 244, 57, 57));
        current_steps.setImageDrawable(unreach);
    }

    if (check_current_cals < check_current_target) {
        current_cals.setColorFilter(Color.argb(255, 244, 57, 57));
        current_target.setColorFilter(Color.argb(255, 244, 57, 57));
        current_cals.setImageDrawable(unreach);
        current_target.setImageDrawable(unreach);
    }
}

排查方向

  • 确认TextView的真实文本内容:转换数值前,输出ucc和uct的完整内容,检查是否包含空格、逗号、单位(如"kcal")等非数字字符,这些会导致转换后的数值和视觉显示不符。
  • 输出转换后的实际数值:在if语句前添加日志,查看check_current_cals和check_current_target的具体数值,确认两者的大小关系是否真的满足check_current_cals < check_current_target。示例代码:
    Log.d("CalCheck", "当前卡路里:" + check_current_cals + " | 目标卡路里:" + check_current_target);
    
  • 检查后续代码的覆盖操作:确认是否有其他代码(如异步回调、生命周期方法)在这个if语句之后,修改了current_cals或current_target的Drawable/颜色滤镜,导致设置被覆盖。
  • 验证ImageView的可修改性:将第二个if内的代码单独放到按钮点击事件中执行,确认ImageView是否能正常显示修改后的状态,排除控件或Drawable本身的问题。
  • 添加数值转换的异常捕获:用try-catch包裹数值转换代码,避免TextView文本为空或非法时,转换失败导致数值异常(比如默认0),影响判断逻辑。示例:
    double check_current_cals = 0;
    try {
        String ucc = user_current_cals.getText().toString().trim();
        check_current_cals = Double.parseDouble(ucc);
    } catch (NumberFormatException e) {
        Log.e("CalError", "卡路里转换失败:" + e.getMessage());
    }
    
    double check_current_target = 0;
    try {
        String uct = user_current_target.getText().toString().trim();
        check_current_target = Double.parseDouble(uct);
    } catch (NumberFormatException e) {
        Log.e("TargetError", "目标值转换失败:" + e.getMessage());
    }
    

内容的提问来源于stack exchange,提问作者HSN I

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 16:22:04