无法为我的android应用程序创建ID值资源
其中无法为我的android应用程序创建ID值资源,android,android-studio,android-relativelayout,android-constraintlayout,Android,Android Studio,Android Relativelayout,Android Constraintlayout,其中setContentView(R.laout.activity\u main)起作用。。。 findViewByID没有,它说无法解析 这是一个基本的应用程序,可以更改背景颜色 AppCompatActivity { Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS
setContentView
(R.laout.activity\u main
)起作用。。。
findViewByID
没有,它说无法解析
这是一个基本的应用程序,可以更改背景颜色
AppCompatActivity {
Button button;
@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.WHITE);
myButtonListenerMethod();
}
public void myButtonListenerMethod() {
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);
}
}
});
}
}````
这是错误的RelativeLayout bgElement=(RelativeLayout)findViewById(R.id.activity_main)代码>将布局文件误认为是id
这样修吧
RelativeLayout bgElement = findViewById(R.id.relative_layout_id);
用正确的相对布局id替换相对布局id