嘗試在FRAGMENT中使用Butterknife時,我得到java.lang.NullPointerException
。我錯過了什麼?在片段中使用Butterknife時發生的java.lang.NullPointerException
@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");
}
如果我使用鍋爐板代碼以聲明tv_detail_startTime
,它的工作原理。只要使用Bind
就會發生錯誤。
完整的堆棧跟蹤:
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)
你可以發佈LogCat的**實際** stacktrace嗎? –
[什麼是空指針異常,以及如何解決它?]可能重複(http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do -i-fix-it) – wero
@BuhakeSindi完成。 – user1406716