在 Android 中使用 Butterknife
Using Butterknife in Android
我正在尝试在 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();
}
我想你忘了这个
ButterKnife.bind(this);
有关详细信息,请阅读 Butternife
> 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.
我正在尝试在 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();
}
我想你忘了这个
ButterKnife.bind(this);
有关详细信息,请阅读 Butternife
> 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.