【发布时间】: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