我的错误是:“找不到符号变量activity_main” 怎么解决呢?此代码用于更改Android屏幕的亮度 Java代码是 我使用了不同的方法,但无法解决。
package com.example.lightbluered;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends 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.RED);
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);
}
}
});
}
}
和活动主要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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524" />
</androidx.constraintlayout.widget.ConstraintLayout>
该怎么解决?
答案 0 :(得分:1)
根据此更改布局:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity"
android:id="@+id/relative"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524" />
</Relativelayout>
然后更改此行
RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main);
到
RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.relative);
您无需在onclick方法中再次定义relativelayout
答案 1 :(得分:0)
在您的:
RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main);
您正尝试引用布局XML文件中不存在的R.id.activity_main
,
尝试将布局设置为:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/activity_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
答案 2 :(得分:0)
只需将代码更改为...
package com.example.lightbluered;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ConstraintLayout bgElement = (ConstraintLayout ) findViewById(R.id.cl);
bgElement.setBackgroundColor(Color.RED);
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);
}
}
});
}
}
还有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:layout_width="match_parent"
android:id="@+id/cl"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案 3 :(得分:0)
如果您需要使用ConstraintLayout执行所需的操作,则可以使用以下布局,
您只需要在当前布局中添加async
。我将其命名为await
。
id
然后您的代码应像这样。
constraintLayout
但是如果您需要使用RelativeLayout进行此操作,也可以这样做。
在<?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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.524" />
</androidx.constraintlayout.widget.ConstraintLayout>
内添加package com.example.lightbluered;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
ConstraintLayout bgElement;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
bgElement = (ConstraintLayout) findViewById(R.id.constraintLayout);
bgElement.setBackgroundColor(Color.RED);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int color = ((ColorDrawable)
bgElement.getBackground()).getColor();
if (color == Color.RED) {
bgElement.setBackgroundColor(Color.BLUE);
} else {
bgElement.setBackgroundColor(Color.RED);
}
}
});
}
}
。但这取决于您选择。您可以删除RelativeLayout
并将其设置为ConstraintLayout
或如下所示。
ConstraintLayout
然后您的代码应像这样。
RelativeLayout
答案 4 :(得分:0)
您正在初始化视图,因此应使用“ R.id”。然后添加您的视图ID,并且您也没有在RelativeLayout
中定义任何activity_main.xml
来解决此问题,因此将activity_main.xml
的内容更改为此:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</RelativeLayout>
,然后在MainActivity.java
中将RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.activity_main);
更改为RelativeLayout bgElement = (RelativeLayout) findViewById(R.id.relative_layout);
答案 5 :(得分:0)
您只犯了一个错误..也就是说,您正在尝试使用XML文件中不存在的相对布局。现在你可以做两件事, 1.在XML文件中将“约束”布局更改为“相对”布局。 2.在Java文件中将“相对”布局更改为“约束”布局。