【发布时间】:2016-08-14 01:02:00
【问题描述】:
我大约两天前安装了Android Studio,我还安装了Java和JDK 1.8的最新版本(我认为是Java SE 8u101)。我能够编辑代码,并且正在学习 Android Studio 官方网站上的教程。 (这里:https://developer.android.com/training/basics/firstapp/building-ui.html)
但是,在点击运行时,我得到了以下两个错误:
-
错误:(24, 57) 错误: 找不到符号变量 activity_display_message
代码:
ViewGroup 布局 = (ViewGroup) findViewById(R.id.activity_display_message);
-
错误:(33, 25) 错误: 找不到符号变量EXTRA_MESSAGE
代码:
intent.putExtra(EXTRA_MESSAGE, message);
我可能做错了什么?模拟器弹出,我可以访问手机上的其他应用程序,但我无法加载我的应用程序。我应该安装 Java SE 8u102 吗?
我还收到第二条错误消息:
错误:任务 ':app:compileDebugJavaWithJavac' 执行失败。 > 编译失败;有关详细信息,请参阅编译器错误输出。
感谢任何帮助。
下面是我的第一个 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>
下面是我的“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>
【问题讨论】:
-
检查你使用 setContentView(layoutId) 膨胀的布局。然后去layout看看是否有id为activity_display_message的view。显然不是。
-
第二个错误可以忽略,因为它是前两个错误的结果。要弄清楚前两个错误发生了什么,您可以发布布局的实际 xml 吗?这就是错误 1 的问题所在,并且很可能导致 # 2 错误(33,25)
-
@UğurB 我认为应该包括在内。我直接从教程中复制并粘贴了所有内容,因为我将其用作练习。我仍然有同样的两个错误。
标签: java android android-studio