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

标签 java android xml

<强>!!请注意!!错误不会发生在setContentView()方法的调用中。在搜索答案时,我发现有人在这里发布了完全相同的问题(完全相同的代码可能来自完全相同的教程源和所有内容),但它被标记为重复并错误地定向到问题是不匹配类型的帖子在 setContentView() 方法中而不是 findViewByID() 中,解决方案是将“R.id.activity_main”更改为“R.layout.activity_main”,但此处并非如此。作为记录,无论如何我都试过了,但它只是将错误消息更改为“嘿,这需要是‘id’”!

=== 问题 ===
目前我的代码中仅有的 2 个错误都指向不同方法中的相同语句
RelativeLayout bgElement = findViewById(R.id.activity_main);
其中 activity_main 为红色并显示消息“无法解析符号‘activity_main’”
清理和重建时编译错误是:error:
找不到符号变量 activity_main

这是我的第一个android编程项目,我也从来没有用过xml,所以请慢点说,用小话。哈哈

=== 研究/尝试修复 ===
1) import android.R 从未出现在我的代码中。
2)清理和重建并不能解决问题。 (每次尝试修复后我都会清理并重建)
3) 我下面的示例代码在 Android Studio 警告的方法调用前进行了类型转换,警告它是多余的,所以我删除了它。然后一篇帖子建议类型转换是必要的,所以我试着把它加回去。当存在和不存在转换时,错误仍然存​​在。
4) 有人说在重建之前尝试删除 R 文件,但是他们说的地方没有 R.java - 也许它指的是旧版本的 Android Studio?

===Java代码===

package com.example.app1redbluelight;

import android.support.v7.app.AppCompatActivity;
import android.widget.RelativeLayout;
import android.widget.Button;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;

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.WHITE);
        myButtonListenerMethod();
    }

    public void myButtonListenerMethod() {
        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"?>
<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=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="148dp"
        android:layout_marginStart="148dp"
        android:text="Button"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="231dp" />
</android.support.constraint.ConstraintLayout>

最佳答案

在 Android 中,该函数 findViewById() 返回与该布局中的 id 对应的 View 。
在您的代码中,setContentView(R.layout.activity_main); 设置主 Activity 的布局。
这时候findViewById()找到activity_main布局中的view。
所以它无法解析 activity_main 布局中的“activity_main”。
如果您需要获取 activity_main 的布局,请尝试以下操作。

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  ConstraintLayout bgElement = (ConstraintLayout)inflater.inflate(R.layout.activity_main, null);

如果需要在activity_main.xml中获取layout,需要设置layout的id。 (必须添加 android:id = "@+id/activity_main"。)
请参阅以下内容。

<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"
android:id = "@+id/activity_main"
tools:context=".MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="148dp"
    android:layout_marginStart="148dp"
    android:text="Button"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="231dp" />
</android.support.constraint.ConstraintLayout>

关于java - findViewById(R.id.activity_main) --> 无法解析符号 'activity_main',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50710460/

相关文章:

java - 如何使用 Selenium 点击 google 上的特定结果?

java - 使用 jdom 从 xml 文件中删除元素

javascript - 从数据:text/xml调用XSLT文件

java - 未过期的谷歌日历api channel java

java - 根据使用 Java 8 流的元素之间的差异拆分数字的有序列表多个列表

java - 使用自定义适配器创建列表时出现“无空构造函数”logcat 错误

安卓图像按钮

java - 未找到 android.support.v4.content.FileProvider

安卓工作室 : table layouts conten reformats when text is cahnged

java - 在黑莓中将长字符串转换为字符串数组?