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

问题描述 投票:0回答:5

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

===问题===
目前我的代码中仅有的两个错误都指向不同方法中的相同语句

RelativeLayout bgElement = findViewById(R.id.activity_main);

其中,activity_main 为红色,并显示消息“无法解析符号‘activity_main’”
清理和重建时编译错误为:error:
cannot find symbol variable 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>
java android xml
5个回答
2
投票

在 Android 中,该函数 findViewById() 返回与该布局中的 id 对应的视图。
在您的代码中, setContentView(R.layout.activity_main); 设置 mainactivity 的布局。
此时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中获取布局,则需要设置布局的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>

1
投票

您正在尝试查找 xml 文件中不存在的布局:

RelativeLayout bgElement = /*(RelativeLayout)*/findViewById(R.id.activity_main);

您的 xml 文件中没有任何元素具有 id“activity_main”,您的 xml 中也没有relativelayout。

大多数错误来自于缺少 id 设置为 Activity_main 的元素。

我想你想改变整个屏幕的背景颜色,所以在ConstraintLayout中添加一个id:

<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>

然后更改代码以使用它:

ConstraintLayout bgElement = /*(ConstraintLayout)*/findViewById(R.id.activity_main);

0
投票

问题就是由这些原因造成的。
1:附加的 xml 中没有名为“activity_main”的 id。
findViewbyId() 会在该 xml 文件中查找 View,因为您将 Activity_main.xml 设置为 MainActivity。
2 :xml内容中没有RelativeLayout项。

修复
尝试将 id“activity_main”添加到 ConstraintLayout,
或尝试使用 LayoutInflator。


0
投票

发现以这种方式调用activity_main并不理想,并且会因布局约束而导致应用程序崩溃。

插入一个新的水平布局,然后引用该布局来更改背景颜色。 Android似乎不太喜欢用这种方式去过多地改变约束层的变量。

希望这有帮助


0
投票

就您而言,布局不需要任何 id。仅将 getWindow() 用于您的目的。我修改了你的代码如下:

public class MainActivity extends AppCompatActivity {    
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RelativeLayout bgElement = findViewById(R.id.activity_main);
    // add your code here
    getWindow().getDecorView().setBackgroundColor(Color.RED);
    myButtonListenerMethod();
}
@SuppressLint("SetTextI18n")
private void myButtonListenerMethod() {
    button = findViewById(R.id.button);
    ColorDrawable bgColor = (ColorDrawable) getWindow().getDecorView().getBackground();
    button.setOnClickListener(v -> {
        if (bgColor.getColor() == Color.RED) {
            getWindow().getDecorView().setBackgroundColor(Color.GREEN);
            button.setText("Convert to red!");
        } else if (bgColor.getColor() == Color.GREEN) {
            getWindow().getDecorView().setBackgroundColor(Color.RED);
            button.setText("Convert to green!");
        }
    });
}

}

© www.soinside.com 2019 - 2024. All rights reserved.