setbackgroundcolor正在使android应用程序崩溃

xqk2d5yq  于 2021-07-12  发布在  Java
关注(0)|答案(3)|浏览(367)

我最近做了一个小应用程序,当产生一定数量的钱时(这里是100000),它应该改变应用程序的背景色和文本。但是,每当我超过10000时,应用程序就会崩溃。我不知道为什么,也绕不过去。我看到这个问题只与背景色有关,因为它在其他方面正确地显示文本。
以下是活动的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);
                }
            }
        });
    }
}
cwdobuhd

cwdobuhd1#

makeMoney = findViewById(R.id.moneyGenerator);
    moneyText=findViewById(R.id.money);
    richAlert=findViewById(R.id.richTag);

你找到了所有的风景,除了背景( ConstraintLayout ),这就是为什么它会扔 NullPointerException (很可能是因为您没有附加日志)。
你得先找到布局

background = findViewById(R.id.Trial);

onCreate .

6yoyoihd

6yoyoihd2#

你还没有在上面定义“背景”视图。要使用.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));
xmq68pz9

xmq68pz93#

background = findViewById(R.id.Trial);

只要找到布局和空指针excation将被删除

相关问题