我正在尝试更改我的工具栏主题,但是当我执行应用程序崩溃时。
通过查看关于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: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"/>
活动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。 有什么建议吗? 提前致谢
更新1 在尝试@vikas kumar建议的方法后更新了堆栈跟踪,遗憾的是仍然遇到崩溃:/
答案 0 :(得分:1)
从您的样式 AppTheme.Toolbar
中删除此行<item name="android:transitionName" tools:ignore="NewApi">toolbar</item>
你很高兴
旧式:
<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>
新风格:
<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>