为什么按下按钮时背景颜色不会改变?

时间:2017-10-31 06:00:51

标签: android android-layout

问题 下面是一个简单的android程序中的xml和主要活动文件,用于在单击按钮时更改屏幕的背景颜色。

为什么是以下代码生成以下错误,修复它的最佳方法是什么?我认为这个问题与id activity_main有关,但我不确定如何解决它。我已经在这个网站上看到了类似的问题而没有成功。

错误 java.lang.NullPointerException:尝试调用虚方法' void android.widget.RelativeLayout.setBackgroundColor(int)'在com.beutifultec.strobelight.MainActivity.onCreate(MainActivity.java:32)的空对象引用上

这是

bgElement.setBackgroundColor(Color.RED);
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout 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.beutifultec.strobelight.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.widget.RelativeLayout>
public class MainActivity extends AppCompatActivity {
public void myButtonListenerMethod() {
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RelativeLayout bgElement = (RelativeLayout)findViewById(R.id.activity_main);
            int color = ((ColorDrawable) bgElement.getBackground()).getColor();
            if (color == Color.RED) {
                bgElement.setBackgroundColor(Color.BLUE);
            }
            else { bgElement.setBackgroundColor(Color.RED);
            }
        }
    });
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main);
    bgElement.setBackgroundColor(Color.RED);
    myButtonListenerMethod();
}

如果更改代码以更改按钮的颜色而不是主显示,确切的代码可以正常工作。

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button bgElement = (Button) findViewById(R.id.button);
    bgElement.setBackgroundColor(Color.RED);
    myButtonListenerMethod();
}

public void myButtonListenerMethod() {
    Button button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button bgElement = (Button) findViewById(R.id.button);
            int color = ((ColorDrawable) bgElement.getBackground()).getColor();
            if (color == Color.RED) {
                bgElement.setBackgroundColor(Color.BLUE);
            }
            else {
                bgElement.setBackgroundColor(Color.RED);
            }
        }
    });
}
}

1 个答案:

答案 0 :(得分:1)

为根视图添加ID,在这种情况下为RelativeLayout

<android.widget.RelativeLayout
    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/root_layout"           // <- Add this line here
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.beutifultec.strobelight.MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.56"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

</android.widget.RelativeLayout>

RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main);更改为RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.root_layout);

这应该有用。

注意:从XML中删除评论

相关问题