NullPointerException with using ButterKnife

末鹿安然 提交于 2019-12-12 01:18:02

问题


I enable annotation processing like this and follow this code:

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;
}
}

I have complex structure:

<?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>

This 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.access$800(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.access$800(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) 

Oh, yeah, I use this gradle version -> 'com.jakewharton:butterknife:8.0.1'

But I don't know why arise NullPointerException. I searched through Internet in search of a problem, but solutions and have not found. Does anyone help me? Thank you.


回答1:


I think you forgot to Return your 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.
}
}

Edit 1 :

@Bind replaces @InjectView and @InjectViews. ButterKnife.bind and ButterKnife.unbind replaces ButterKnife.inject and ButterKnife.reset respectively.

And Also Add this.

TextView temperature_max = ButterKnife.findById(view, R.id.temperature_max);

Look at this Reference Here.




回答2:


You should use @Bind instead of @BindView. Everything else is fine.



来源:https://stackoverflow.com/questions/36977646/nullpointerexception-with-using-butterknife

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!