gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:09:15 26 4
gpt4 key购买 nike

我正在尝试更改我的工具栏主题,但是当我这样做时应用程序崩溃了。

看过关于 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)

样式.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>

工具栏.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 :
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。
有什么建议么?
我的 Activity 扩展了 AppCompatActivity。

提前致谢!

最佳答案

试试这个

<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>

然后在 list 中使用 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" />

关于Android:设置工具栏主题会导致应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45803399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com