java Android Studio 找不到符号变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38938297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio cannot find symbol variable
提问by Jeff
I installed Android Studio about two days ago, I also installed the most updated version of Java and JDK 1.8 (I think it was Java SE 8u101). I am able to edit code and I'm following a tutorial from the official Android Studio website. (Here: https://developer.android.com/training/basics/firstapp/building-ui.html)
我大约两天前安装了 Android Studio,我还安装了最新版本的 Java 和 JDK 1.8(我认为是 Java SE 8u101)。我能够编辑代码,并且正在学习官方 Android Studio 网站上的教程。(这里:https: //developer.android.com/training/basics/firstapp/building-ui.html)
However, upon hitting run I've gotten the two following errors:
但是,在点击运行时,我收到了以下两个错误:
Error:(24, 57) error: cannot find symbol variable activity_display_message
Code for this:
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
Error:(33, 25) error: cannot find symbol variable EXTRA_MESSAGE
Code for this:
intent.putExtra(EXTRA_MESSAGE, message);
错误:(24, 57) 错误:找不到符号变量activity_display_message
代码:
的ViewGroup布局=(的ViewGroup)findViewById(R.id。activity_display_message);
错误:(33, 25) 错误:找不到符号变量 EXTRA_MESSAGE
代码:
intent.putExtra(EXTRA_MESSAGE,消息);
What might I be doing wrong? The emulator pops up and I can get to other apps on the phone but I just can't get my app to load. Should I have installed Java SE 8u102 instead?
我可能做错了什么?模拟器弹出,我可以访问手机上的其他应用程序,但我无法加载我的应用程序。我应该安装 Java SE 8u102 吗?
I also got a second error message:
我还收到了第二条错误消息:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.
错误:任务 ':app:compileDebugJavaWithJavac' 的执行失败。> 编译失败;有关详细信息,请参阅编译器错误输出。
Any help is appreciated.
任何帮助表示赞赏。
Below is my code for the first xml file, "activity_main.xml":
下面是我的第一个 xml 文件“activity_main.xml”的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
Below is my code for "activity_display_message.xml"
下面是我的“activity_display_message.xml”代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.appno1.DisplayMessageActivity">
</RelativeLayout>
回答by BluePie
Add this to your MainActivity:
将此添加到您的 MainActivity:
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
You might have missed this line, which can be found in the "Build an Intent" part.
您可能错过了这一行,它可以在“构建意图”部分中找到。
Refer this for more,from the original page.
从原始页面参考更多信息。
回答by Crabime
I think you might lose the last note in the tutorial.
so you just add android:id attribute to the activity_display_message.xml file and it will be ok!oh actually you should add into the layout element which is the valid location.
所以你只需将 android:id 属性添加到 activity_display_message.xml 文件就可以了!哦实际上你应该添加到布局元素中,这是有效的位置。
回答by HPV121
My advice is, try to clean the project and build it again. If you want to access newly added resources you need to build the project. Cleaning will help to remove all previously autogenerated resources from project and building will help to create them again + new ones.
我的建议是,尝试清理项目并重新构建它。如果要访问新添加的资源,则需要构建项目。清理将有助于从项目中删除所有以前自动生成的资源,而构建将有助于再次创建它们 + 新资源。
回答by AnglusWang
You should add EXTRA_MESSAGE
as static final, like:
您应该添加EXTRA_MESSAGE
为静态最终,例如:
private static final String EXTRA_MESSAGE = "message";
回答by n00b
Regarding this error,
关于这个错误,
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
ViewGroup 布局 = (ViewGroup) findViewById(R.id.activity_display_message);
check that the file res > layout > activity_display_message.xml as the id specified as android:id="@+id/activity_display_message"
检查文件 res > layout > activity_display_message.xml 作为 id 指定为 android:id="@+id/activity_display_message"