【问题标题】:Using Butterknife in Android在 Android 中使用 Butterknife
【发布时间】:2017-11-21 06:50:53
【问题描述】:

我正在尝试在 Android 中使用 Butterknife,它似乎不起作用。你能指导我哪里做错了吗?

我尝试在“OnClick”中放置一个调试点,但似乎没有。

Gradle 依赖项(应用程序)

 compile 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

activity_main.xml

   <LinearLayout
        android:id="@+id/action_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@+id/fragment_container"
        android:weightSum="2">
        <Button
            android:id="@+id/btn_frg_one"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Fragment One"/>
        <Button
            android:id="@+id/btn_frg_two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Frag Two"/>
    </LinearLayout>

MainActivity.java

@OnClick({R.id.btn_frg_one, R.id.btn_frg_two})
    public void addFrgToCon(View view){
        switch (view.getId()){
            case R.id.btn_frg_one:
                addFragment(new FOne());
                break;
            case R.id.btn_frg_two:
                addFragment(new FTwo());
                break;
        }
    }

    public void addFragment(Fragment fragment){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

【问题讨论】:

标签: android onclick butterknife


【解决方案1】:

我想你忘了这个

ButterKnife.bind(this);

更多信息请阅读Butternife

【讨论】:

  • @BRDroid 你添加了这个 ButterKnife.bind(this);
  • 这就是问题所在。谢谢Prem,您能否更新您的答案,我会接受它
【解决方案2】:
> use ButterKnife.bind(this);
> 
> 
> Other provided binding APIs:
> 
>     Bind arbitrary objects using an activity as the view root. If you use a pattern like MVC you can bind the controller using its activity
> with ButterKnife.bind(this, activity).
> 
>     Bind a view's children into fields using ButterKnife.bind(this). If you use <merge> tags in a layout and inflate in a custom view
> constructor you can call this immediately after. Alternatively, custom
> view types inflated from XML can use it in the onFinishInflate()
> callback.

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多