android - 构建错误 : Unable to find a particular resource

标签 android xml android-studio compiler-errors

当我运行下面的代码时,我得到了这个错误,并得到了多个位置,但是这个代码通常意味着当我写这样的代码时

C:\Users\saad\Desktop\Sa2d\Android\JustJava\app\src\main\java\com\example\android\justjava\MainActivity.java
Error:(21, 76) error: cannot find symbol variable fab
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

这是我在“content_main.xml”上的代码

    `<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    android:orientation="vertical"
    tools:showIn="@layout/activity_main">

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Quantity"/>
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="0"/>
   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Order"
       android:id="@+id/button" />
</LinearLayout>`

这是我在“MainActivity.java”上的代码 我用 * 标记错误行

`*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });`

这个问题的解决方案是什么???

最佳答案

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//etc..

是指FloatingActionButton,所以可能你已经删除了xml文件中的代码,然后你必须删除相对于Fab的代码(java)或者你可以添加:

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

在你的 layout.xml 上

关于android - 构建错误 : Unable to find a particular resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34932023/

相关文章:

android - 在 Android (Moto Z) 上禁用自动图像稳定?

php解析简单的xml属性

excel - 从 DOCX4j 中的 xml 中删除不必要的命名空间

android - 如何通过快捷方式导航到 Android Studio 中的错误行?

android - 在 Android Studio 上导入模块

java - 安卓工作室 : debug application on device

Android setContentView 或 Intent ?

android - 如果 android jetpack compose 中的单词太长,则用连字符打破句子行

android - lockCanvas 方法在游戏循环中偶尔出现卡顿/滞后

java - qt项目代码到xml文件转换