我不問了解決方案,但想知道爲什麼TextView
內部工具欄返回null? 我在片段內使用Butterknife。TextView的工具欄空與Butterknife
@BindView(R2.id.toolbarTitle)
TextView toolbarTitle;
這裏是我的佈局:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_widget"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorAccent"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="@+id/toolbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="@dimen/text_size18"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
這裏是我的logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
好這裏的要求代碼:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_nav_bookmark_trend_my_quote, container, false);
toolbarTitle.setText(getString(R.string.bookmarks));
accessToken = AppSharedPreferences.getsharedprefInstance(getContext()).getStringValue(AppSharedPreferences.KEY_USER_TOKEN);
mUnbinder = ButterKnife.bind(this, view);
mLinearLayout = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLinearLayout);
DrawerLayout drawer = ((EpicSwiperActivity) getActivity()).drawerLayout;
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
getActivity(), drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
addRecyclerViewScrollState(mRecyclerView);
return view;
}
郵政編碼的片段你在哪裏使用textview –
什麼是R2的意思? –
用途:'toolbarTitle.setText(R.string.your_title);' –