Android:设置工具栏主题会导致应用崩溃

时间:2017-08-21 18:19:31

标签: android android-layout crash toolbar android-toolbar

我试图更改我的工具栏主题,但是当我执行应用程序崩溃时。

在SO上看了一个类似的问题,似乎我做得对。我也试图一步一步地追随tutorial,但没有成功 如果未设置主题,则会显示工具栏并且应用程序可以正常工作。但是如果设置了工具栏主题,则应用程序会崩溃并显示以下堆栈跟踪:

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: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"/>

活动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
}

应用程序正在尝试设置工具栏标题,但出于某种原因会发生NPE。 有什么建议? 我的活动扩展了AppCompatActivity。

提前致谢!

1 个答案:

答案 0 :(得分:0)

试试这个

<resources>
    <style name="MyMaterialTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>
    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:itemBackground">#ffffff</item>
        <item name="android:textColorSecondary">#ffffff</item>
    </style>
</resources>

然后在清单中更改当前主题android:theme="@style/MyMaterialTheme"

在toolbar.xml中更改

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary" />