2016-11-22 57 views
0

我有一个CoordinatorLayout在那里我有我的按钮:Butterknife不与作品还包括标签

<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,其中包括我的按钮:

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

在这里,我有一个片段与Butterknife:

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'(fields)或'@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包含标签的ID吗? –

+0

不是jst id,你需要通过那个id的视图我猜 – Neji

0

我建议您使用Google提供的Data Binding Library而不是Butterknife

+0

为什么databinding?目前它不能和jack一起工作 –

+1

Jack对任何东西都不起作用。虽然我同意,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; 
    } 
} 

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

这就是它!

信用: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

简单删除ID从包括标签和绑定将工作,即使在包含布局的领域。