无法通过java更改背景颜色

问题描述 投票:0回答:4

我试图进行一个设置活动,用户可以从中更改主要活动的背景颜色...这里是我的java代码,xml的所有内容都很好,目标活动改变了背景而不是收音机的那个纽扣

public class SettingsActivity extends AppCompatActivity {


ConstraintLayout loginContainer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    loginContainer = findViewById(R.id.loginContainer);
}

public void onColorRadioClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    RadioButton btn1 = findViewById(R.id.colorRadio1);
    RadioButton btn2 = findViewById(R.id.colorRadio2);
    RadioButton btn3 = findViewById(R.id.colorRadio3);

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.colorRadio1:
            if (checked)
                btn2.setChecked(false);
                btn3.setChecked(false);
                loginContainer.setBackgroundColor(getResources().getColor(R.color.wallBack1));
                break;
        case R.id.colorRadio2:
            if (checked)
                btn1.setChecked(false);
                btn3.setChecked(false);
                loginContainer.setBackgroundColor(getResources().getColor(R.color.wallBack2));
            break;
        case R.id.colorRadio3:
            if (checked)
                btn1.setChecked(false);
                btn2.setChecked(false);
                loginContainer.setBackgroundColor(getResources().getColor(R.color.wallBack3));
            break;
    }
}

}

每当我点击一个单选按钮,首先(不试图改变颜色)一切正常但现在我添加这些行后,每次我点击它崩溃说它无法执行android的方法:onClick

“运行”部分中的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.roeie.renote, PID: 2590
              java.lang.IllegalStateException: Could not execute method for android:onClick
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                  at android.view.View.performClick(View.java:5610)
                  at android.widget.CompoundButton.performClick(CompoundButton.java:122)
                  at android.view.View$PerformClick.run(View.java:22260)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Method.invoke(Native Method)
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                  at android.view.View.performClick(View.java:5610) 
                  at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
                  at android.view.View$PerformClick.run(View.java:22260) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.constraint.ConstraintLayout.setBackgroundColor(int)' on a null object reference
                  at com.example.roeie.renote.SettingsActivity.onColorRadioClicked(SettingsActivity.java:34)
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                  at android.view.View.performClick(View.java:5610) 
                  at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
                  at android.view.View$PerformClick.run(View.java:22260) 
                  at android.os.Handler.handleCallback(Handler.java:751) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Radio Buttons XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.roeie.renote.SettingsActivity"
android:background="@color/wallBack1">

<RadioButton
    android:onClick="onColorRadioClicked"
    android:id="@+id/colorRadio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="40dp"
    android:text="RadioButton"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<RadioButton
    android:onClick="onColorRadioClicked"
    android:id="@+id/colorRadio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="RadioButton"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/colorRadio1" />

<RadioButton
    android:onClick="onColorRadioClicked"
    android:id="@+id/colorRadio3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="RadioButton"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/colorRadio2" />

</android.support.constraint.ConstraintLayout>

需要更改的活动的xml是

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/loginContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/wallBack1"
tools:context="com.example.roeie.renote.LoginActivity">

<TextView

    android:id="@+id/textView"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:text="RENOTE"
    android:textAlignment="center"
    android:textColor="@color/fui_bgGoogle"
    android:textSize="80sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.223" />

<Button
    android:id="@+id/signInBtn"
    android:layout_width="230dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:background="@drawable/button_shape_1"
    android:onClick="signInClick"
    android:text="Sign In"
    android:textColor="@color/wallBack1"
    app:layout_constraintBottom_toTopOf="@+id/signOutBtn"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.977" />

<Button
    android:id="@+id/signOutBtn"
    android:layout_width="230dp"
    android:layout_height="wrap_content"
    android:background="@drawable/button_shape_1_rev"
    android:onClick="signOutClick"
    android:text="Sign Out"
    android:textColor="#FFFFFF"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.928" />

<Button
    android:id="@+id/button2"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="28dp"
    android:background="@drawable/ic_action_settings_white"
    android:onClick="settingsBtnClick"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
<!--android:background="@drawable/button_shape_1"-->

java android android-studio
4个回答
0
投票

您想要更改其背景的根中的ConstraintLayout中缺少您的ID。

机器人:ID = “@ + ID / loginContainer”

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.roeie.renote.SettingsActivity"
android:id="@+id/loginContainer"
android:background="@color/wallBack1">

<RadioButton
    android:onClick="onColorRadioClicked"
    android:id="@+id/colorRadio1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="40dp"
    android:text="RadioButton"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<RadioButton
    android:onClick="onColorRadioClicked"
    android:id="@+id/colorRadio2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="RadioButton"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/colorRadio1" />

<RadioButton
    android:onClick="onColorRadioClicked"
    android:id="@+id/colorRadio3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:text="RadioButton"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/colorRadio2" />

</android.support.constraint.ConstraintLayout>

编辑:如果要从设置活动更改MainActivity背景,请将其保存在某个首选项中,并在主活动处于活动状态时应用onStart()。

要么

您可以以startActivityForResult()开始设置活动,然后从意图数据中获取onActivityResult()中选择的背景drawable,然后更改它。

编辑:示例代码

首先,像这样开始你的设置活动。

  Intent selectColor = new Intent(this, SettingsActivity.class);
  startActivityForResult(selectColor, 101);

选择后调用此方法并传递所选的颜色ID

public void setImageSelected(int colorID){
Intent resultIntent = new Intent();
resultIntent.putExtra("SELECTED_COLOR", colorID);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}

然后像这样得到结果。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == 101) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
           //here get the selected id using
           int colorID = data.getIntExtra("SELECTED_COLOR", 0);
          //use it to set the background here
        }
    }
}

获得背景颜色

int colorSelected = ContextCompat.getColor(this,R.color.selectedColor);

0
投票

用qazxsw poi包装你的if语句

{}

0
投票

从onColorRadioClicked方法中删除您的ID

 switch(view.getId()) {
            case R.id.colorRadio1:
                if (checked){
                    btn2.setChecked(false);
                    btn3.setChecked(false);
                    loginContainer.setBackgroundColor(getResources().getColor(R.color.wallBack1));
                    }
                    break;
            case R.id.colorRadio2:
                if (checked){
                    btn1.setChecked(false);
                    btn3.setChecked(false);
                    loginContainer.setBackgroundColor(getResources().getColor(R.color.wallBack2));
                }
                break;
            case R.id.colorRadio3:
                if (checked){
                    btn1.setChecked(false);
                    btn2.setChecked(false);
                    loginContainer.setBackgroundColor(getResources().getColor(R.color.wallBack3));
                  }
                break;
        }
    }   

并把它放在OnCreate中


0
投票

在OnCreate方法中

RadioButton btn1 = findViewById(R.id.colorRadio1);
RadioButton btn2 = findViewById(R.id.colorRadio2);
RadioButton btn3 = findViewById(R.id.colorRadio3);

在XML中

loginContainer = (ConstraintLayout)findViewById(R.id.loginContainer);
RadioButton btn1 = (RadioButton )findViewById(R.id.colorRadio1);
RadioButton btn2 = (RadioButton )findViewById(R.id.colorRadio2);
RadioButton btn3 = (RadioButton )findViewById(R.id.colorRadio3);
© www.soinside.com 2019 - 2024. All rights reserved.