Java setBackgroundColor正在使android应用程序崩溃
我最近制作了一个小应用程序,当产生一定数量的钱时(这里是100000),它应该改变应用程序的背景颜色和文本。但是,每当我超过10000时,应用程序就会崩溃。我不知道为什么,也绕不开它。我发现这个问题只与背景色有关,因为它正确地显示了文本 下面是活动的XMLJava setBackgroundColor正在使android应用程序崩溃,java,android,android-studio,android-layout,crash,Java,Android,Android Studio,Android Layout,Crash,我最近制作了一个小应用程序,当产生一定数量的钱时(这里是100000),它应该改变应用程序的背景颜色和文本。但是,每当我超过10000时,应用程序就会崩溃。我不知道为什么,也绕不开它。我发现这个问题只与背景色有关,因为它正确地显示了文本 下面是活动的XML <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xm
<?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代码(省略了包,因为它会占用很多空间。请告诉我是否有人想阅读它。)
公共类MainActivity扩展了AppCompatActivity{
私人按钮赚钱;
私人int totalMoney=0;
私有文本查看moneyText;
私有文本视图richAlert;
私人青年背景;
NumberFormat currency=NumberFormat.getCurrencyInstance(新语言环境(“en”、“in”));
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
赚钱=findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
makeMoney.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
总货币+=1000;
moneyText.setText(currency.format(totalMoney));
Log.d(“MoneyTag”,“Money Maked:”+totalMoney);
Toast.makeText(getApplicationContext(),“Total Money=\u20B9”+Toast Money,Toast.LENGTH\u LONG)
.show();
如果(总金额>=10000){
setText(“欢迎来到moneyland bro您还没有在上面定义“背景”视图。
background = findViewById(R.id.Trial);
要使用.setBackgroundColor,您需要在某些视图中使用它
尝试创建布局视图的对象,然后更改该布局的颜色
background = findViewById(R.id.Trial);
mConstraintLayout=
(约束)findviewbyd(R.id.Trial);
赚钱=findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
如果(总金额>=10000){
欢迎来到moneyland bro
background = findViewById(R.id.Trial);
您正在查找除后台(ConstraintLayout
)之外的所有视图,这就是为什么它会抛出NullPointerException
(很可能是因为您没有附加日志)。
你需要先找到布局
在onCreate
中
只需查找布局和空指针例外将被删除请将您的登录添加到变量background
实际初始化的任何位置?明白了。谢谢。但是为什么我需要使用onCreate?setBackgroundColor可以设置背景色您正在使用findViewById
在onCreate
中找到视图。之后,变量background
有一个指向布局的实际指针,您可以在代码中随时使用它。