vinoth kumar vinoth kumar - 5 months ago 36
Android Question

android.support.v7.widget.Toolbar.getTitle()' on a null object reference

Open the new activity on my app that time i get a below error.


java.lang.CharSequence android.support.v7.widget.Toolbar.getTitle()


My xml code:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.v7.widget.Toolbar
android:id="@+id/order_detail_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/appcolor"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>




My java code:


private Toolbar toolbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.order_detail);
String orderid = getIntent().getStringExtra("order_detail_toolbar");

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("DDIT_Results");

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}


Any one know the solution please explain the error and give the solution.

Answer

you are trying to get the Toolbar with wrong id.

you've set the id to be

android:id="@+id/order_detail_toolbar"

but are trying to get it on

toolbar = (Toolbar) findViewById(R.id.toolbar);

so change that line to toolbar = (Toolbar) findViewById(R.id.order_detail_toolbar);