
!!请注意 !!在调用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="@R_301_6822@://schemas.androID.com/apk/res/androID" xmlns:app="@R_301_6822@://schemas.androID.com/apk/res-auto" xmlns:tools="@R_301_6822@://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="@R_301_6822@://schemas.androID.com/apk/res/androID" xmlns:app="@R_301_6822@://schemas.androID.com/apk/res-auto" xmlns:tools="@R_301_6822@://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); 总结 以上是内存溢出为你收集整理的java-findViewById(R.id.activity_main)->无法解析符号’activity_main’全部内容,希望文章能够帮你解决java-findViewById(R.id.activity_main)->无法解析符号’activity_main’所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)