4

I am getting java.lang.NullPointerException when trying to use Butterknife in a FRAGMENT. What am I missing?

@Bind(R.id.tv_detail_startTime) TextView tv_detail_startTime;

@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    v = inflater.inflate(R.layout.fragment_detail_expanlv,
            container, false);
    ButterKnife.bind(getActivity());

    //THIS IS THE LINE WHERE 
    tv_detail_startTime.setText("Trying Butterknife out in Android");

}

If I use boiler plate code to declare tv_detail_startTime, it works. Just using the Bind gives the error.

Complete Stacktrace:

10-29 04:30:28.971 24953-24953/com.mavdev.focusoutfacebook E/AndroidRuntime:   FATAL EXCEPTION: main
Process: com.mavdev.focusoutfacebook, PID: 24953
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at     com.mavdev.focusoutfacebook.fragments.scheduledblocks.Fragment_detail_expanlv.onCreateView(Fragment_detail_expanlv.java:424)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Parker_Halo
  • 505
  • 2
  • 19
user1406716
  • 9,565
  • 22
  • 96
  • 151
  • Can you post the **actual** stacktrace from LogCat? – Buhake Sindi Oct 29 '15 at 08:28
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – wero Oct 29 '15 at 08:29
  • Then what line of code can you find this? `com.mavdev.focusoutfacebook.fragments.scheduledblocks.Fragment_detail_expanlv.onCreateView(Fragment_detail_expanlv.java:424)` – Buhake Sindi Oct 29 '15 at 08:32
  • @BuhakeSindi As I state in the question, this is the line 424: `tv_detail_startTime.setText("Trying Butterknife out in Android");` – user1406716 Oct 29 '15 at 08:33
  • There you go: `tv_detail_startTime` is `null`. Can you debug to see why? – Buhake Sindi Oct 29 '15 at 08:34
  • exactly, shouldn't `@Bind(R.id.tv_detail_startTime) TextView tv_detail_startTime;` be enough to declare that textview. I am using the Butterknife library, what else is needed. – user1406716 Oct 29 '15 at 08:36

2 Answers2

9

Bind the fragment not the activity

View v = inflater.inflate(R.layout.xxx, container, false);
ButterKnife.bind(this, v);
Laurent Russier
  • 658
  • 5
  • 24
0

ButterKnife probably assumes you are binding the activity view to an activity, not a fragment. Please try using ButterKnife.bind(this, getActivity()) (notice the extra this).

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • I assumed you wanted to bind a View which is in your parent activity, and not the fragment you are just creating. The answer of Laurent Russier assumes that the view is in the fragment. Use either one depending on your use-case. – Daniel Zolnai Oct 29 '15 at 08:44