Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java findViewById(R.id.activity_main)-->;无法解析符号';主要活动';_Java_Android_Xml - Fatal编程技术网

Java findViewById(R.id.activity_main)-->;无法解析符号';主要活动';

Java findViewById(R.id.activity_main)-->;无法解析符号';主要活动';,java,android,xml,Java,Android,Xml,!!请注意调用setContentView()方法时不会发生错误。在搜索答案时,我发现有人在这里发布了完全相同的问题(完全相同的代码可能来自完全相同的教程源和所有内容),但它被标记为重复,并错误地指向一篇文章,其中问题是setContentView()方法中的不匹配类型,而不是findViewByID()解决方案是将“R.id.activity\u main”更改为“R.layout.activity\u main”,但这里的情况并非如此。对于记录,我尝试过,但它只是改变了错误消息为“嘿,这需要

!!请注意调用setContentView()方法时不会发生错误。在搜索答案时,我发现有人在这里发布了完全相同的问题(完全相同的代码可能来自完全相同的教程源和所有内容),但它被标记为重复,并错误地指向一篇文章,其中问题是setContentView()方法中的不匹配类型,而不是findViewByID()解决方案是将“R.id.activity\u main”更改为“R.layout.activity\u main”,但这里的情况并非如此。对于记录,我尝试过,但它只是改变了错误消息为“嘿,这需要是'id'”

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

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

==研究/尝试修复===
1) 导入android.R从未出现在我的代码中。
2) 清理和重建并不能修复它。(每次尝试修复后,我都会进行清理和重建)
3) 我下面的示例代码在androidstudio警告的方法调用前面有类型转换,这是多余的,所以我删除了它。后来有一篇帖子建议演员阵容是必要的,所以我试着把它加回去。当铸件存在或不存在时,错误仍然存在。
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 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文件===

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

在Android中,该函数findViewById()返回与该布局中的id对应的视图。
在代码中,setContentView(R.layout.activity_main)设置主活动的布局。
此时,findViewById()会在activity_主布局中查找视图。
因此,它无法解析activity\u main布局中的“activity\u main”。
如果需要获取活动的布局,请尝试以下操作

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  ConstraintLayout bgElement = (ConstraintLayout)inflater.inflate(R.layout.activity_main, null);
<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>
如果需要在activity_main.xml中获取布局,则需要设置布局的id。(android:id=“@+id/activity_main”必须添加。)
见下文

 LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  ConstraintLayout bgElement = (ConstraintLayout)inflater.inflate(R.layout.activity_main, null);
<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中没有名为“activity\u main”的id。
findViewbyId()在该xml文件中查找视图,因为您将activity_main.xml设置为MainActivity。
2:xml内容中没有RelativeLayout项

修复
尝试将id“activity_main”添加到ConstraintLayout,

或者尝试使用LayoutInflator。

发现以这种方式调用activity\u main并不理想,并且会由于布局约束而导致应用程序崩溃

插入新的水平布局,然后参照该布局以更改背景色。Android似乎不喜欢以这种方式尝试过多地更改约束层的变量


希望这有帮助

您没有相对论我希望我帮助了@MattWaits:)在您发布的文件中,activity_main指的是布局文件和id为activity_main的视图组件。在您发布的xml文件中,没有id为“activity_main”的视图,因此您会出现错误。我已经指出,为了使其正常工作,您需要将“activity_main”添加到xml中的一个元素。根据我对您的代码的理解,您似乎想要更改activivvty的背景色,因此,应该将缺少的id添加到最外层的视图元素,即ConstraintLayout。这就是为什么我建议更改代码并添加id的原因。嗯,我认为activity_main指的是xml文件的名称,但您的意思是它应该作为id添加到xml文件中吗?本教程从未提到编辑xml文件,所以我只是假设我需要的所有内容都是在创建项目时自动添加的。奇怪的是,教程没有提到这一点。哦,好吧。你的解决方案很有效,非常感谢。你是我的英雄,非常感谢安德鲁。这确实是我的问题。听到这个消息我很高兴。