【问题标题】:build error: Unable to find a particular resource构建错误:找不到特定资源
【发布时间】:2016-01-21 18:53:53
【问题描述】:

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

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 id fab
  • 是的,我注意到了,谢谢

标签: android xml android-studio compiler-errors


【解决方案1】:
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 上

【讨论】:

    【解决方案2】:

    在布局中添加一个名为 fab 的浮动操作按钮。

    例子:

    <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" />
    

    【讨论】:

      【解决方案3】:

      您的布局没有任何FloatingActionButton id fab

      【讨论】:

        【解决方案4】:

        在您的 build.gradle 文件中,添加以下内容:

        compile 'com.android.support:design:23.1.1'
        

        更多参考

        FloatingActionButton example with Support Library

        【讨论】:

          【解决方案5】:

          尝试添加以下 Gradle 依赖项:

          编译'com.android.support:design:22.2.0'

          之后是 gradle 同步。

          【讨论】:

          • 我 10 分钟前给出的相同答案.. 你加了什么加分?
          猜你喜欢
          • 2022-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-04-24
          • 1970-01-01
          • 1970-01-01
          • 2011-12-27
          相关资源
          最近更新 更多