java - setBackgroundColor 正在使 android 应用程序崩溃
问题描述
我最近做了一个小应用程序,当产生一定数量的钱(这里是 100000)时,它应该改变应用程序的背景颜色和文本。但是,每当我超过 10000 时,应用程序就会崩溃。我不知道为什么也无法绕过它。我看到这个问题仅与背景颜色有关,因为它可以正确显示文本。
这是 activity_main 的 XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Trial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EC5353"
tools:context=".MainActivity">
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:gravity="center"
android:text="@string/Title"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="@string/moneyPresent"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/Title" />
<Button
android:id="@+id/moneyGenerator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:background="#FFFFFF"
android:backgroundTint="#732424"
android:backgroundTintMode="multiply"
android:text="@string/generateMoneyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/money" />
<TextView
android:id="@+id/richTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="@string/grind_boi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/moneyGenerator" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的 Java 代码(省略了这些包,因为它会占用很多空间。如果有人想阅读它,请告诉我。)
public class MainActivity extends AppCompatActivity {
private Button makeMoney;
private int totalMoney=0;
private TextView moneyText;
private TextView richAlert;
private ConstraintLayout background;
NumberFormat currency=NumberFormat.getCurrencyInstance(new Locale("en","in"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
makeMoney.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
totalMoney+=1000;
moneyText.setText(currency.format(totalMoney));
Log.d("MoneyTag", "Money Made: "+totalMoney);
Toast.makeText(getApplicationContext(),"Total Money= \u20B9"+totalMoney,Toast.LENGTH_LONG)
.show();
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro ");
background.setBackgroundColor(16776960);
}
}
});
}
}
解决方案
您还没有在上面定义“背景”视图。要使用 .setBackgroundColor,您需要在某些视图中使用它
尝试制作布局视图的对象,然后更改该布局的颜色
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.Trial);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro ");
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
推荐阅读
- java - android studio基本布局上不显示按钮和textview
- visual-studio - Visual Studio 2019 图标扩展和拼写检查错误
- local - Minikube - 外部 IP 与主机的公共 IP 不匹配
- python - 在 Flask 中获取上传的文件大小
- python-3.x - 解析xml时的字符识别问题
- javascript - 反应 - 选择导致页面向下滚动
- ios - 我可以为 iOS 自动续订订阅提供可选的免费试用吗?
- mysql - 正则表达式替代
- python - Python颜色/格式在pyinstaller中不起作用
- react-native - 在 React Native 中打开推送通知时如何导航到屏幕?