【问题标题】:ButterKnife 8.6.0 not workingButterKnife 8.6.0 不工作
【发布时间】:2017-06-15 13:02:54
【问题描述】:

我之前使用的是 7.0.1,它工作正常,但突然停止工作,使用变量时出现空指针异常。然后我将其更改为 8.6.0

我的代码是这样的。

(项目分级)

dependencies {
    classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'    
}

(应用程序分级)

apply plugin: 'com.jakewharton.butterknife'

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

内部片段

public class ScheduleRideFragment extends Fragment {
@BindView(R.id.scheduleRideFragApplyCouponButton)
Button applyCouponButton;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = LayoutInflater.from(getActivity()).inflate(R.layout.schedule_ride_fragment_layout, null);
    ButterKnife.setDebug(true);
    ButterKnife.bind(this, view);
    initAndSetListners();
    return view; 
}
private void initAndSetListners() {
applyCouponButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
 }
}

但它给出了 java.lang.NullPointerException(见日志)

注意:我只贴了相关代码

我也看过这些1,2,3 答案,但不起作用 它正在发生两种情况(片段和活动)我做错了吗?

无法发布整个 schedule_ride_fragment_layout.xml 代码,所以我只发布相关代码

<?xml version="1.0" encoding="utf-8"?>

<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="wrap_content">
<LinearLayout
android:id="@+id/scheduleRideCouponButtonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp"
android:visibility="invisible">
<Button
android:id="@+id/scheduleRideFragApplyCouponButton"
android:layout_width="wrap_content"
android:layout_height="35sp"
android:background="@color/brightGreen"
android:ems="12"
android:text="APPLY COUPON"
android:textColor="@color/textColor" /></LinearLayout></android.support.design.widget.CoordinatorLayout>`

【问题讨论】:

  • 试试这个LayoutInflater.from(getContext()).inflate(R.layout. schedule_ride_fragment_layout, container, false)
  • 感谢@Luca Nicoletti 的回复,但没有成功
  • 请张贴 schedule_ride_fragment_layout.xml
  • @lelloman 你是在询问按钮 ID,我已经检查了 R.id.scheduleRideFragApplyCouponButton 是否存在于 xml 中。两天前它工作正常,我没有改变任何东西
  • 是的,你是对的,升级库后你做了干净的构建吗?

标签: android nullpointerexception butterknife


【解决方案1】:

Butterknife 8.6.0开始:

(project gradle)

中移除 build.gradle 中的类路径插件
dependencies {
       classpath 'com.jakewharton:butterknife-gradle-plugin:8.6.0'
}

并且仅在 (app gradle) 中的 build.gradle 中保留以下依赖项,如official documentation

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

最后对于onClickListener()使用Butterknife提供的OnClick注解,

@OnClick(R.id.scheduleRideFragApplyCouponButton)
public void onApplyCouponButtonClick(Button button){
}

【讨论】:

  • 先生,您的代码和OP的代码有什么显着区别?你的代码解决了 OP 的代码有什么问题?
  • @Prokash Sarkar 也试试这个,但最初它没有给出错误,还有一些字段也像 TextView 和 EditText ,一些错误正在出现,我想知道它是否从过去 2 个月开始工作为什么它停止工作。
  • 请也发布您的 xml 布局的代码@AjeetChoudhary
  • 实际上,他保留了官方文档不推荐的依赖插件。我怀疑可能存在依赖冲突。 @azizbekian
  • 我认为我们使用了相同的依赖项,但它仍然不起作用
猜你喜欢
  • 2016-08-29
  • 2018-05-09
  • 2016-11-15
  • 1970-01-01
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多