无法通过java更改背景颜色

时间:2017-12-24 16:20:25

标签: java android android-studio

我试图进行设置活动,用户可以从中更改主要活动的背景颜色...这是我的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"-->

4 个答案:

答案 0 :(得分:0)

使用{}

删除你的if语句
 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;
        }
    }   

答案 1 :(得分:0)

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

  

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

Command failed on instance. Return code: 126 Output: /bin/sh: .ebextensions/supervise.sh:
Permission denied.

修改:如果您想从设置活动更改 MainActivity 背景,请将其保存在某些偏好设置中并应用主要的 onStart()活动时,活动。

或者

您可以将设置活动作为 startActivityForResult()开始,然后从意图数据中选择 onActivityResult()中的背景可绘制,然后进行更改。

编辑:示例代码

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

<?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>
选择后

调用此方法并传递选定的颜色ID

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

然后像这样得到结果。

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
        }
    }
}

答案 2 :(得分:0)

从onColorRadioClicked方法中删除你的id

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

并将其放入OnCreate

答案 3 :(得分:0)

在OnCreate方法

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);

在XML

android:id="@+id/loginContainer"
相关问题