【问题标题】:Android Studio cannot find symbol variableAndroid Studio 找不到符号变量
【发布时间】: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)

但是,在点击运行时,我得到了以下两个错误:

  1. 错误:(24, 57) 错误: 找不到符号变量 activity_display_message

    代码:

    ViewGroup 布局 = (ViewGroup) findViewById(R.id.activity_display_message);

  2. 错误:(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


【解决方案1】:

将此添加到您的 MainActivity:

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

You might have missed this line, 可以在“构建意图”部分找到。

Refer this for more, 来自原始页面。

【讨论】:

    【解决方案2】:

    我认为您可能会丢失教程中的最后一个音符。

    所以你只需将 android:id 属性添加到 activity_display_message.xml 文件就可以了!哦实际上你应该添加到有效位置的布局元素中。

    【讨论】:

      【解决方案3】:

      我的建议是,尝试清理项目并重新构建它。如果要访问新添加的资源,则需要构建项目。清理将有助于从项目中删除所有以前自动生成的资源,而构建将有助于再次创建它们+新资源。

      【讨论】:

        【解决方案4】:

        你应该添加EXTRA_MESSAGE作为静态final,比如:

        private static final String EXTRA_MESSAGE = "message";
        

        【讨论】:

        • 这样就摆脱了第一个错误,谢谢!关于第二部分“activity_display_message”有什么建议吗?
        【解决方案5】:

        关于这个错误,

        ViewGroup 布局 = (ViewGroup) findViewById(R.id.activity_display_message);

        检查文件 res > layout > activity_display_message.xml 作为 id 指定为 android:id="@+id/activity_display_message"

        【讨论】:

          猜你喜欢
          • 2015-11-24
          • 1970-01-01
          • 2017-08-18
          • 2020-06-30
          • 1970-01-01
          • 2016-04-06
          • 1970-01-01
          • 2016-03-02
          • 1970-01-01
          相关资源
          最近更新 更多