【问题标题】:Cannot find symbol variable activity_main找不到符号变量 activity_main
【发布时间】:2017-07-06 14:07:54
【问题描述】:

我是新人。每当我尝试构建和运行此代码时,都会出现此消息。

这里是 XML 代码:

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:orientation="vertical"
tools:context="com.example.android.justjava.MainActivity">

<TextView
    android:id="@+id/quantity_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="quantity"
    android:textAllCaps="true"
    android:textSize="16sp"
    android:textColor="@android:color/black"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"/>

<TextView
    android:id="@+id/zero_text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0"
    android:layout_below="@+id/quantity_text_view"
    android:textSize="16sp"
    android:textColor="@android:color/black"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="order"
    android:textAllCaps="true"
    android:layout_below="@+id/zero_text_view"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:onClick="submitOrder"/>

这里是 Java 代码:

/** * 重要提示:在下方添加您的包。包名可以在 项目的 AndroidManifest.xml 文件。 * 这是包名 我们的示例使用: * * 包 com.example.android.justjava; * */

导入 android.R;导入android.os.Bundle;进口 android.support.v7.app.AppCompatActivity;导入android.view.View; 导入android.widget.TextView;

导入静态 com.example.android.justjava.R.id.quantity_text_view;

/** * 此应用显示订购咖啡的订单。 */ 上市 类 MainActivity 扩展 AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
}

/**
 * This method is called when the order button is clicked.
 */
public void submitOrder(View view) {
    display(1);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id/quantity_text_view);
    quantityTextView.setText("" + number);
} }

这是错误的图片:

https://i.stack.imgur.com/tlrUi.png

请帮帮我

【问题讨论】:

    标签: java android xml android-layout android-studio


    【解决方案1】:
    1. 检查您的 xml 文件名。
    2. 检查您的进口声明
    3. 清理并再次构建项目。

    4. 如果上述方法不起作用,则删除 R 文件并清理它

    【讨论】:

    • 谢谢。当我删除 R 文件并且错误消息消失但现在出现另一个问题是“错误:包 R 不存在”时该怎么办?
    • 只需删除已经导入的 R 包并手动导入您的应用程序
    【解决方案2】:

    在这里检查包名是否匹配 工具:context="com.example.android.justjava.MainActivity"

    其他问题是您的 findValueById 中的“\”代替了“。”

    【讨论】:

      【解决方案3】:

      准确提供布局的文件名。我认为您的布局文件名是activity_main而不是main_activity。

      【讨论】:

      • 图中显示了两个错误。一个是重命名为 activity_main 它会关闭,另一个是在显示函数中。R.id.quantity_text_view 而不是 R.id/quantity...
      • 是的,我检查了。上面的评论就是答案
      • 已更改,但问题仍然出现! link
      • 将其改回 R.layout.main_activity 并进行清理并再次构建它
      【解决方案4】:

      您的显示功能如下所示。

      private void display(int number) { TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view); quantityTextView.setText("" + number); } }

      findValueById 中有“\”代替“.”

      【讨论】:

      • 当我添加 .而不是 / 出现错误图片添加:link
      • 能否分享一下.xml文件名
      【解决方案5】:

      去掉这行 tools:context="com.example.android.justjava.MainActivity" 此行用于活动名称,但我认为您复制粘贴 MainActivity.java 的 XML 并且该布局不是 MainActivity。你应该删除这行它不会产生任何错误。

      【讨论】:

        【解决方案6】:

        在屏幕右侧的app-->res-->Layout,检查activity的名称可能是activity_main_java.xml,所以你必须替换R.layout。 R.layout.activity_main_java

        的 main_activity

        【讨论】:

          【解决方案7】:

          检查 Resource(R) 的导入。即指向其他模块或库。

          【讨论】:

            猜你喜欢
            • 2021-02-18
            • 2023-04-02
            • 1970-01-01
            • 2015-09-14
            • 2016-07-01
            • 2016-03-27
            • 2010-10-12
            • 2012-03-20
            • 1970-01-01
            相关资源
            最近更新 更多