2016-01-21 40 views
1

當我運行下面的代碼,我得到這個錯誤,並與多個位置,但與此代碼得到它通常是指當我寫這樣的代碼編譯錯誤:無法找到特定的資源

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(); 
     } 
    });` 

這個問題的解決方案是什麼?

+0

您的佈局沒有任何帶有'fab'的'FloatingActionButton'。 – thetonrifles

+0

是的,我觀察到謝謝你 –

回答

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

是指FloatingActionButton,所以也許你已經刪除了這些代碼的XML文件,然後你必須刪除代碼(JAVA)相對於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" /> 

您layout.xml

+1

謝謝你的工作 –

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

也謝謝你的工作 –

1

您的佈局沒有任何FloatingActionButton,編號爲fab

-1

嘗試增加以下搖籃依賴性:

編譯「com.android.support:設計:22.2.0'

然後是gradle sync。

+0

同樣的答案給我10分鐘前..你添加了什麼額外的點? –