2016-11-22 2 views
0

У меня есть CoordinatorLayout, где у меня есть моя кнопка:нож для масла не работает с Включают тег

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="bottom|end"> 

    <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_marginBottom="16dp" 
     android:layout_marginRight="16dp" 
     android:src="@drawable/icon" 
     app:fabSize="normal"/> 

</android.support.design.widget.CoordinatorLayout> 

И тогда я имею XML Activity, где я включаю кнопку:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    ... 

     <include 
      android:id="@+id/fabHere" 
      layout="@layout/fab_here" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

</LinearLayout> 

И здесь я есть фрагмент с ножом для масла:

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: Требуемый вид 'fab' с ID 2131624072 для поля 'fabHere' не найден. Если это представление необязательно, добавьте примечание «@Nullable» (поля) или «@Optional» (методы).

Я пытался добавить аннотаций в '@Nullable' и '@optional', но он не работает

+0

Что произойдет, если вы удалите 'android: id =" @ + id/fabHere "'? – Blackbelt

+0

ничего не происходит –

+0

Что именно вы имеете в виду, ничего не происходит? Это рухнет, разве это ничего не показывает? Используйте 'Butterknife.setDebug (true)' –

ответ

0

Пожалуйста, посетите Butterknife issue

Автор @JakeWharton говорит -

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 идентификатор тега include? –

+0

не jst id, вам нужно передать представление об этом id i guess – Neji

0

I Предложите использовать Data Binding Library, предоставленный Google, вместо Butterknife

+0

Почему привязка данных? В настоящий момент это не работает с гнездом –

+1

Джек не работает ни с чем. Хотя я согласен, DBL не нужен для решения этой проблемы. – EpicPandaForce

0

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; 
    } 
} 

XML, в MainActivity:

<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, из Включенные Layout:

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

Вот так!

Кредит: Reference

+0

Этот код не проверен, шаг 5 написано «ButerKnife», отсутствует «t» –

+0

этот код не работает, я тестировал – siva

-1

У меня была такая же проблема, Моя проблема была решена таким образом

шаг 1:

комментарий

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

и

// ButterKnife.bind(this, view); 

шаг 2:

FloatingActionButton fab; 

связывание в onCreateView

запустить приложение

шаг 3:

удалить до кода!

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


ButterKnife.bind(this, view); 

чистый проект и перестроить проект

запустить приложение

0

Простой удалить идентификатор из тега включают и связывание будет работать, даже для полей в прилагаемом макете.