微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

!!请注意 !!在调用setContentView()方法时不会发生该错误.在搜索答案时,我发现有人在这里发布了完全相同的问题(可能与教程源和所有内容完全相同),但被标记为重复并且错误地定向到问题是不匹配类型的帖子在setContentView()方法中而不是findViewByID()中,解决方案是将“ R.id.activity_main”更改为“ R.layout.activity_main”,但这不是这种情况.为了记录,我还是尝试了一下,但是它只是将错误消息更改为“嘿,这需要是’id’”!

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

这是我的第一个android编程项目,而且我也从未使用过xml,因此请慢速讲话并使用小词.大声笑

===研究/尝试修复===
1)导入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>

解决方法:

您正在尝试查找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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。