2016-11-22 17 views
0

を含めますButterknifeとフラグメントがあります。Butterknifeは動作しませんが、私は私が私のボタンを持っているCoordinatorLayoutを持っているタグ

public class MyFragment extends Fragment { 
.... 
@BindView(R.id.fab) FloatingActionButton fab; 


    @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      view = inflater.inflate(R.layout.my_fragment, container, false); 
      ButterKnife.bind(this, view); 

      return view; 
    } 
... 
} 

をしかし、私はアプリを起動する場合は、このが表示されます。
java.lang.IllegalStateException:フィールド 'fabHere'のID 2131624072の必須ビュー 'fab'が見つかりませんでした。このビューがオプションの場合は、 '@ Nullable'(フィールド)または '@Optional'(メソッド)アノテーションを追加します。

は、私が '@Nullable' と '@optional' 注釈を追加しようとしましたが、それは

+0

'android:id =" @ + id/fabHere "'を削除するとどうなりますか? – Blackbelt

+0

何も起こらない –

+0

何も起こらないということはどういう意味ですか?それはクラッシュする、それは何も表示されませんか? 'Butterknife.setDebug(true) 'を使用してください –

答えて

0

が@JakeWhartonが言うButterknife issue

作者をご覧ください動作しません -

Put an ID on the <include> and bind it to an arbitrary View field for each one. 
Then create an object which binds the inner views 

static class Something { 
    @Bind(R.id.something) TextView somethingView; 
} 

You can then create multiple instances of that object 
and call ButterKnife.bind(something1, include1) and 
ButterKnife.bind(something2, include2) and so on. 

Consider also just making a custom view instead of using <include> which has 
proper APIs to get its children (or at least binds them to fields itself). 
+0

githubでもこれが見つかりました。しかし、それを理解していない。 –

+0

はinclude1のidですか? –

+0

jst idでない場合、そのIDのビューを渡す必要があります。 – Neji

0

Iバターナイフの代わりにGoogleが提供するData Binding Libraryを使用するようにお勧めします。

+0

なぜデータバインディングですか?それはジャックでは機能しません。 –

+1

ジャックは何も動作しません。私は同意しますが、この問題を解決するためにDBLは必要ありません。 – EpicPandaForce

0

The MainActivity、

public MainActivity extends AppCompatActivity { 
    // 1. First, we declare the layout that was included as a View objects. 
    @BindView(R.id.layout_1) View layout_1; 
    @BindView(R.id.layout_2) View layout_2; 

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

     // 2. In here, we bind the included layouts 
     ButterKnife.bind(this); 

     // 4. Then, we create objects of the type of the IncludedLayout. 
     //  In this example the layout reuse the same layout twice, so, there are two 
     //  IncludedLayouts. 
     IncludedLayout includedLayout_1 = new IncludedLayout(); 
     IncludedLayout includedLayout_2 = new IncludedLayout(); 

     // 5. We bind the elements of the included layouts. 
     ButterKnife.bind(includedLayout_1, layout_1); 
     ButterKnife.bind(includedLayout_2, layout_2); 

     // 6. And, finally, we use them. 
     includedLayout_1.displayed_text.setText( "Hello"); 
     includedLayout_2.displayed_text.setText( "Hey!"); 
    } 

    // 3. We create a static class that will be an container of the elements 
    //  of the included layout. In here we declare the components that 
    //  hold this. In this example, there is only one TextView. 
    static class IncludedLayout { 
     @BindView(R.id.displayed_text) TextView displayed_text; 
    } 
} 

MainActivityのXML:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <include android:id="@+id/layout_1" layout="@layout/included_layout" /> 
     <include android:id="@+id/layout_2" layout="@layout/included_layout" /> 
</LinearLayout> 

含まれるレイアウトのXML:それだ

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/displayed_text"/> 
</LinearLayout> 

クレジット:私は同じ問題を抱えていた

+0

このコードはテストされていません。ステップ5は "ButerKnife"と綴られています。 "t"がありません。 –

+0

このコードは機能していません。 – siva

-1

Referenceは、 私の問題は、このような方法で解決された

ステップ1:

コメント

// @BindView(R.id.fab) FloatingActionButton fab; 

// ButterKnife.bind(this, view); 

ステップ2:

コードを削除します。onCreateView

ランアプリ

ステップ3を結合

FloatingActionButton fab; 

を!

@BindView(R.id.fab) FloatingActionButton fab; 


ButterKnife.bind(this, view); 

クリーンプロジェクトと

0

シンプルタグと結合も含まれるレイアウトのフィールドに、動作しますが含まからIDを削除するプロジェクト

実行アプリを再構築します。