错误无法解析 Android Studio 中的符号 activity_main

穆罕默德

我的错误是:“找不到符号变量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>

在此处输入图片说明 怎么解决呢?

蒙塔西尔·奥尼克

根据此更改您的布局:

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

不需要relativelayout在onclick方法中再次定义

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在android studio中删除activity_main(small / xLarge / large)

如何在Android Studio中更改默认布局(activity_main)

Android Studio MyFirstApp教程中的错误“无法解析符号editText”

无法解析 Android Studio 中的符号“来自”错误

findViewById(R.id.activity_main)->无法解析符号'activity_main'

Android Studio错误:无法解析符号“视图”

RecyclerView“无法解析符号”错误,-Android Studio

Android Studio无法解析AAR中的符号

无法在Android Studio中解析符号“注释”?

无法在Android Studio中解析符号“ LocationClient”

无法在Android Studio中解析符号WebRTC

无法在 android studio 中解析符号“coordinatorlayout”

无法在Android Studio中解析符号“ GCMBroadcastReceiver”

无法在Android Studio中解析符号

无法在Android Studio中解析符号getReadableDatabse

无法在Android Studio中解析符号GeoPoint

无法在Android Studio中解析某些符号

错误:“无法解析符号'@ string / edit_message'“ Android Studio

无法解析Android Studio上的原始符号错误

Android Studio数据绑定错误,无法解析符号

Android Studio:错误:无效的恶魔和“R”符号无法解析

如何解决“无法解析符号‘V7’Android Studio中的错误

在Android Studio中无法解析符号“ R”:“清单合并失败,出现多个错误,请参阅日志”

activity_main无法解析或不是字段

activity_main无法解析或不是字段

Android Studio无法解析符号

无法解析符号Android Studio

错误:无法解析activity_main或没有字段

无法在Android Studio中解析符号HttpGet,HttpClient,DefaultHttpClient