Can't find textview in toolbar include layout android -
i have toolbar has custom textview inside:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:background="@drawable/headerbg" android:theme="@style/customtoolbartheme" android:layout_height="48dp"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="toolbar title" android:textcolor="@color/white" android:textsize="18sp" android:fontfamily="sans-serif-light" android:layout_gravity="left" android:id="@+id/tvtoolbartitle" /> </android.support.v7.widget.toolbar>
i use include in layout:
<include android:id="@+id/app_bar" layout="@layout/app_bar" />
i reference toolbar , textview set text:
toolbar toolbar = (toolbar) activity.findviewbyid(r.id.toolbar); logger.d(activity.getclass(), "toolbar: " + toolbar); textview tvtoolbartitle = (textview) toolbar.findviewbyid(r.id.tvtoolbartitle); tvtoolbartitle.settext(activity.gettitle()); activity.setsupportactionbar(toolbar);
i keep getting error:
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.support.v7.widget.toolbar.findviewbyid(int)' on null object reference
if remove id of the include tag, works perfectly.
you overriding id of toolbar in include tag. r.id.toolbar
no longer id of included toolbar activity r.id.app_bar
instead.
if keep id in include tag use following code access toolbar:
toolbar toolbar = (toolbar) activity.findviewbyid(r.id.app_bar);
Comments
Post a Comment