使用 ButterKnife 时出现 NullPointerException
NullPointerException with using ButterKnife
我启用注释处理,如 this 并遵循此代码:
public class MainFragment extends Fragment implements WeatherUpdater.AsyncResponse {
@BindView(R.id.temperature_max) TextView temperature_max;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ButterKnife.bind(this, view);
temperature_max.setText("NULL POINTER!");
...
return view;
}
}
我的结构很复杂:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView>
<RelativeLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
<RelativeLayout>
<RelativeLayout>
<TextView>
<TextView
android:id="@+id/temperature_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20sp" />
</RelativeLayout>
...
</ScrollView>
这个LOGCAT:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app, PID: 22813
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.ui.activity.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5158)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.my.app.ui.fragment.MainFragment.onCreateView(MainFragment.java:28)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:685)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5319)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5158)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
哦,是的,我用这个 gradle 版本 -> 'com.jakewharton:butterknife:8.0.1'
但是不知道为什么会出现NullPointerException。我通过 Internet 搜索问题,但没有找到解决方案。有人帮助我吗?谢谢。
我想你忘了 Return 你的 View.
public class MainFragment extends Fragment implements WeatherUpdater.AsyncResponse {
@BindView(R.id.temperature_max) TextView temperature_max;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ButterKnife.bind(this, view);
temperature_max.setText("NULL POINTER!");
return view; // This need to be return.
}
}
编辑 1 :
@Bind
替换了 @InjectView
和 @InjectViews
。 ButterKnife.bind
和 ButterKnife.unbind
分别替换了 ButterKnife.inject
和 ButterKnife.reset
。
还要加上这个。
TextView temperature_max = ButterKnife.findById(view, R.id.temperature_max);
看看这个参考Here.
您应该使用 @Bind
而不是 @BindView
。其他都还好。
我启用注释处理,如 this 并遵循此代码:
public class MainFragment extends Fragment implements WeatherUpdater.AsyncResponse {
@BindView(R.id.temperature_max) TextView temperature_max;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ButterKnife.bind(this, view);
temperature_max.setText("NULL POINTER!");
...
return view;
}
}
我的结构很复杂:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView>
<RelativeLayout>
<LinearLayout>
</LinearLayout>
<LinearLayout>
<RelativeLayout>
<RelativeLayout>
<TextView>
<TextView
android:id="@+id/temperature_max"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20sp" />
</RelativeLayout>
...
</ScrollView>
这个LOGCAT:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app, PID: 22813
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.ui.activity.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2224)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5158)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.my.app.ui.fragment.MainFragment.onCreateView(MainFragment.java:28)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:685)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.Activity.performStart(Activity.java:5319)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2283)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5158)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
哦,是的,我用这个 gradle 版本 -> 'com.jakewharton:butterknife:8.0.1'
但是不知道为什么会出现NullPointerException。我通过 Internet 搜索问题,但没有找到解决方案。有人帮助我吗?谢谢。
我想你忘了 Return 你的 View.
public class MainFragment extends Fragment implements WeatherUpdater.AsyncResponse {
@BindView(R.id.temperature_max) TextView temperature_max;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ButterKnife.bind(this, view);
temperature_max.setText("NULL POINTER!");
return view; // This need to be return.
}
}
编辑 1 :
@Bind
替换了 @InjectView
和 @InjectViews
。 ButterKnife.bind
和 ButterKnife.unbind
分别替换了 ButterKnife.inject
和 ButterKnife.reset
。
还要加上这个。
TextView temperature_max = ButterKnife.findById(view, R.id.temperature_max);
看看这个参考Here.
您应该使用 @Bind
而不是 @BindView
。其他都还好。