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

3 ответа

Просто попробуйте это

Toolbar toolbar = (Toolbar) findViewById(toolbar_widget);
getActivity().setSupportActionBar(toolbar);

TextView tvTitle = (TextView) toolbar.findViewById(R.id.toolbarTitle);
tvTitle.setText(title);

От ButterKnife просто попробуй это

TextView textView = ButterKnife.findById(toolbar, R.id.toolbarTitle);

{{ РЕДАКТИРОВАТЬ }}

toolbar.xml

<android.support.v7.widget.Toolbar
    android:background="?attr/colorAccent"
    android:id="@+id/toolbar_widget"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <LinearLayout
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <TextView
            android:gravity="center"
            android:id="@+id/toolbarTitle"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textColor="#ffffff"
            android:textSize="@dimen/text_size18"
            android:textStyle="bold" />
    </LinearLayout>
 </android.support.v7.widget.Toolbar>

fragmentlayout.xml

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <include
       android:id="@+id/toolbar_widget"
       layout="@layout/toolbar"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>

в вашем Java файл

View toolbarView = getLayoutInflater().inflate(R.layout.toolbar,null);
TextView textView = ButterKnife.findById(toolbarView, R.id.toolbarTitle);

Вы установили свой toolbar как ActionBar в противном случае сделайте это как:

ToolBar toolbar = (Toolbar) findViewById(R.id.toolbar_widget);
setSupportActionBar(toolbar);

Сопоставьте свой идентификатор TextView с идентификатором, который вы использовали на @BindView,

И так как вы используете фрагмент, убедитесь, что вы передаете в корне представление фрагмента вместе с контекстом, когда вы вызываете ButterKnife.bind(this, rootView);

например

View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    ButterKnife.bind(this, view);
Другие вопросы по тегам