简体   繁体   English

setBackgroundColor 使 Android 应用程序崩溃

[英]setBackgroundColor is crashing the android application

I recently made a small app that should change the background color & text of the application when a certain amount of money is generated (here 100000).我最近做了一个小应用程序,当产生一定数量的钱(这里是 100000)时,它应该改变应用程序的背景颜色和文本。 However, whenever I go past 10000, the application crashes.但是,每当我超过 10000 时,应用程序就会崩溃。 I am not sure why and can not get around it.我不知道为什么,也无法绕过它。 I saw that this problem is with background color only because it is otherwise displaying text correctly.我看到这个问题仅与背景颜色有关,因为它可以正确显示文本。

Here is the XML for activity_main这是 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>

Here is my Java Code (Omitted the packages as it would take a lot of space. Do tell me if anyone wants to read it.)这是我的 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);
                }
            }
        });
    }
}

you haven't defined "background" view in the above.您还没有在上面定义“背景”视图。 to use .setBackgroundColor you need to use it with some view要使用 .setBackgroundColor 你需要在一些视图中使用它

try making object of layout view then change the color of that layout尝试制作布局视图的对象,然后更改该布局的颜色

 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));
    makeMoney = findViewById(R.id.moneyGenerator);
    moneyText=findViewById(R.id.money);
    richAlert=findViewById(R.id.richTag);

You're finding all the views, except background( ConstraintLayout ), that's why it is throwing NullPointerException (Most likely, because you haven't attached logs).您正在找到除背景( ConstraintLayout )之外的所有视图,这就是它抛出NullPointerException的原因(很可能,因为您没有附加日志)。

You need to find the layout first你需要先找到布局

background = findViewById(R.id.Trial);

in onCreate .onCreate

 background = findViewById(R.id.Trial);

只需找到布局和空指针异常将被删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM