Mick Hardins
Mick Hardins

Reputation: 25

Android setting toolbar theme causes app crash

I'm trying to change my toolbar theme, but when i do the app crashes.

By seeing other question on SO, it seems i'm doing things right, i also tried to follow some step by step tutorial(tutorial) but with no success. If no theme is set, the toolbar is shown and the app works. But if a toolbar theme is set, the app crashes with the following stack trace:

Process: com.radiationpressure.mickhardins.mtgrecall.debug, PID: 6840
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radiationpressure.mickhardins.mtgrecall.debug/com.radiationpressure.mickhardins.mtgrecall.MtgRecallActivity_new}: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6121)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
 Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
    at android.content.res.TypedArray.getString(TypedArray.java:202)
    at android.view.View.<init>(View.java:4501)
    at android.widget.TextView.<init>(TextView.java:710)
    at android.widget.TextView.<init>(TextView.java:704)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:62)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:58)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:54)
    at android.support.v7.widget.Toolbar.setTitle(Toolbar.java:756)
    at android.support.v7.widget.ToolbarWidgetWrapper.setTitleInt(ToolbarWidgetWrapper.java:261)
    at android.support.v7.widget.ToolbarWidgetWrapper.setWindowTitle(ToolbarWidgetWrapper.java:243)
    at android.support.v7.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
    at android.support.v7.app.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:223)
    at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
    at com.radiationpressure.mickhardins.mtgrecall.MtgRecallActivity_new.onCreate(MtgRecallActivity_new.java:156)
    at android.app.Activity.performCreate(Activity.java:6682)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6121) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

 

styles.xml:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">#4CAF50</item>
    <item name="colorPrimaryDark">#388E3C</item>
    <item name="colorAccent">#448AFF</item>
    <item name="md_neutral_color">#4CAF50</item>
    <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
    <item name="android:windowContentOverlay">@drawable/bottom_shadow</item>

</style>

<style name="AppTheme.Toolbar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="titleMarginStart">16dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">?actionBarSize</item>
    <item name="android:transitionName" tools:ignore="NewApi">toolbar</item>
    <item name="android:textColorPrimaryInverse">@color/md_white_1000</item>
    <!-- android:textColorPrimary is the color of the title text in the Toolbar  -->
    <item name="android:textColorPrimary">@color/md_white_1000</item>
    <!-- actionMenuTextColor is the color of the text of action (menu) items  -->
    <item name="actionMenuTextColor">@android:color/holo_green_light</item>

</style>

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/md_green_500"
    android:theme="@style/AppTheme.Toolbar"
    android:minHeight="@dimen/toolbar_height"
    app:popupTheme="@style/AppTheme.PopupOverlay"/>

activity onCreate:

protected void onCreate(Bundle savedInstanceState)  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mtg_recall_new_v2);
    activityPresenter.start()
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle("title");
    //other code
}

The app is trying to set toolbar title but for some reason it get NPE. Any suggestion? Thanks in advance

UPDATE 1 Updated stack trace after trying the approach suggested by @vikas kumar, unfortunately still experiencing crashes :/

Upvotes: 0

Views: 836

Answers (1)

vikas kumar
vikas kumar

Reputation: 11018

Remove this line from your styles AppTheme.Toolbar

<item name="android:transitionName" tools:ignore="NewApi">toolbar</item>

and you are good to go

Old style:

   <style name="AppTheme.Toolbar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:titleMarginStart">16dp</item>
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">?actionBarSize</item>
        <item name="android:transitionName" tools:ignore="NewApi">toolbar</item>
        <item name="android:textColorPrimaryInverse">@color/white</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:actionMenuTextColor">@android:color/holo_green_light</item>
    </style>

New Style:

  <style name="AppTheme.Toolbar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:titleMarginStart">16dp</item>
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">?actionBarSize</item>
    <item name="android:textColorPrimaryInverse">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:actionMenuTextColor">@android:color/holo_green_light</item>
</style>

Upvotes: 1

Related Questions