constant expression required when trying to create a switch case block

huangapple go评论61阅读模式
英文:

constant expression required when trying to create a switch case block

问题

`an error keeps saying "constant expression required" preventing me from running the app on a device to further develop the app.

am trying to create a sort of google pay.. so i need the selected amounts. but am getting an error saying "constant expression required".. the variable is declared private and not final so i dont know why am getting this error. the value is not even highlighted showing that it is being used. how can i solve this error. i tried innitialising the variable to zero but it still does not help..
`

包 com.example.greenearth;

导入 android.media.Image;
导入 android.os.Bundle;

导入 androidx.annotation.NonNull;
导入 androidx.annotation.Nullable;
导入 androidx.fragment.app.Fragment;

导入 android.view.LayoutInflater;
导入 android.view.View;
导入 android.view.ViewGroup;
导入 android.widget.ImageView;
导入 android.widget.RadioButton;
导入 android.widget.RadioGroup;
导入 android.widget.RelativeLayout;
导入 android.widget.Toast;

公共类 Donations 扩展 Fragment {

    RadioGroup radioGroup;
    RadioButton minimum;
    RadioButton average;
    RadioButton maximum;
    私有 int google_amount=0;

    RelativeLayout rl;

    @Override
    公共 View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        返回 inflater.inflate(R.layout.fragment_donations, container, false);

    }

    @Override
    公共 void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        超级.onViewCreated(view, savedInstanceState);

        radioGroup=view.findViewById(R.id.select);
        minimum=view.findViewById(R.id.fifty);
        average=view.findViewById(R.id.five_hundred);
        maximum=view.findViewById(R.id.ten_thousand);
        rl=view.findViewById(R.id.donater);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            公共 void onCheckedChanged(RadioGroup group, int checkedId) {
                开关 (checkedId){
                    案例 R.id.fifty:
                        google_amount=50;
                        打破;

                    案例 R.id.five_hundred:
                        google_amount=500;
                        打破;

                    案例 R.id.ten_thousand:
                        google_amount=10000;
                        打破;
                }
            }
        });

        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            公共 void onClick(View v) {

                如果 (google_amount>0) {
                    //googlepay
                } else {
                    Toast.makeText(getContext(),"Please choose an amount", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}
英文:

`an error keeps saying "constant expression required" preventing me from running the app on a device to further develop the app.

am trying to create a sort of google pay.. so i need the selected amounts. but am getting an error saying "constant expression required".. the variable is declared private and not final so i dont know why am getting this error. the value is not even highlighted showing that it is being used. how can i solve this error. i tried innitialising the variable to zero but it still does not help..
`


package com.example.greenearth;
import android.media.Image;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class Donations extends Fragment {
RadioGroup radioGroup;
RadioButton minimum;
RadioButton average;
RadioButton maximum;
private int google_amount=0;
RelativeLayout rl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_donations, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
radioGroup=view.findViewById(R.id.select);
minimum=view.findViewById(R.id.fifty);
average=view.findViewById(R.id.five_hundred);
maximum=view.findViewById(R.id.ten_thousand);
rl=view.findViewById(R.id.donater);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.fifty:
google_amount=50;
break;
case R.id.five_hundred:
google_amount=500;
break;
case R.id.ten_thousand:
google_amount=10000;
break;
}
}
});
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (google_amount>0) {
//googlepay
} else {
Toast.makeText(getContext(),"Please choose an amount", Toast.LENGTH_SHORT).show();
}
}
});
}
}

答案1

得分: 7

从 Android Gradle 插件 8.0.0 开始,默认情况下,您的资源(例如 R.id. ...)不再声明为 final(即常量表达式),以优化构建速度,这是在 switch 语句中使用它们的先决条件:
https://developer.android.com/build/optimize-your-build#use-non-constant-r-classes

如果您想保留旧的行为,您可以在 gradle.properties 文件中添加以下行:

android.nonFinalResIds=false

如果您想通过将其转换为 if/else 语句来修复它,Android Studio 将在 switch 关键字上为您提供帮助:

Ctrl + 1 或 Alt + Enter

英文:

Starting with Android Gradle Plugin 8.0.0, by default, your resources (e.g. R.id. ...) are no longer declared final (i.e. constant expressions) for optimized build speed, which is a prerequisite to be used in switch statements:
https://developer.android.com/build/optimize-your-build#use-non-constant-r-classes

If you want to keep the old behavior, you can add this line in the gradle.properties file:

android.nonFinalResIds=false

If you want to fix it by converting it to if/else statements, Android Studio will help you with

<kbd>Ctrl</kbd> + <kbd>1</kbd> or

<kbd>Alt</kbd> + <kbd>Enter</kbd>

on the switch keyword.

答案2

得分: 2

将以下内容添加到文件 gradle.properties 中并同步:

android.nonFinalResIds=false

这对我有用。

英文:

Put this line

android.nonFinalResIds=false

In this file and sync

gradle.properties

That's works for me

答案3

得分: 1

请将您的开关语句替换为以下的if条件语句:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        if (checkedId == R.id.fifty) {
            google_amount = 50;
        } else if (checkedId == R.id.five_hundred) {
            google_amount = 500;
        } else if (checkedId == R.id.ten_thousand) {
            google_amount = 10000;
        }
    }
});

如果您遇到了"需要常量表达式"的错误,请确保在开关语句中使用的表达式确实是常量表达式。如果您需要评估非常量表达式,您可以考虑使用if-else语句来替代。

英文:

Replace your switch statement with if condition like below

 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.fifty) {
google_amount = 50;
} else if (checkedId == R.id.five_hundred) {
google_amount = 500;
} else if (checkedId == R.id.ten_thousand) {
google_amount = 10000;
}
}
});

If you encounter the "constant expression required" error, ensure that the expression used in the switch statement is indeed a constant expression. If you need to evaluate a non-constant expression, you might consider using if-else statements instead.

huangapple
  • 本文由 发表于 2023年6月8日 17:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定