在主题

时间:2017-10-10 11:31:51

标签: android android-appcompat android-theme toast font-family

每当我显示Toast时,应用都会崩溃。

如果我使用旧版本的AppCompat库或从样式中删除fontFamily,该应用程序可以正常工作。

的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show(); //line 13
}

相关性:

compile 'com.android.support:appcompat-v7:26.1.0'

AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:fontFamily">sans-serif-light</item>
</style>

堆栈追踪:

  

引起:java.lang.ArrayIndexOutOfBoundsException:length = 16;指数= 233                                                                        在android.content.res.StringBlock.get(StringBlock.java:65)                                                                        在android.content.res.XmlBlock $ Parser.getPooledString(XmlBlock.java:458)                                                                        在android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1212)                                                                        在android.content.res.TypedArray.getString(TypedArray.java:202)                                                                        在android.support.v7.widget.TintTypedArray.getString(TintTypedArray.java:143)                                                                        在android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:215)                                                                        在android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:152)                                                                        在android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)                                                                        在android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:81)                                                                        在android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:71)                                                                        在android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)                                                                        在android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)                                                                        在android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)                                                                        在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)                                                                        在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)                                                                        在android.view.LayoutInflater.rInflate(LayoutInflater.java:858)                                                                        在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)                                                                        在android.view.LayoutInflater.inflate(LayoutInflater.java:518)                                                                        在android.view.LayoutInflater.inflate(LayoutInflater.java:426)                                                                        在android.view.LayoutInflater.inflate(LayoutInflater.java:377)                                                                        在android.widget.Toast.makeText(Toast.java:266)                                                                        在io.yarsa.blankapp.MainActivity.onCreate(MainActivity.java:13)                                                                        在android.app.Activity.performCreate(Activity.java:6679)                                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)                                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)                                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                                                                        在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)                                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                                        在android.os.Looper.loop(Looper.java:154)                                                                        在android.app.ActivityThread.main(ActivityThread.java:6126)                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

有没有其他选择,以便我可以使用最新版本的fontFamily库在主题中使用AppCompat属性?

2 个答案:

答案 0 :(得分:7)

在主题中添加字体,如下所示 -

    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
        <item name="android:textViewStyle">@style/TextViewStyle</item>
        <item name="android:buttonStyle">@style/ButtonStyle</item>
    </style>

    <style name="TextViewStyle" parent="android:Widget.TextView">
        <item name="android:fontFamily">sans-serif-light</item>
    </style>

    <style name="ButtonStyle" parent="Widget.AppCompat.Button">
        <item name="android:fontFamily">sans-serif-light</item>
    </style>

答案 1 :(得分:4)

根据适用于fonts in xml的Android开发人员指南

  

将字体添加到样式

     

打开styles.xml,并将fontFamily属性设置为字体文件   你想要访问。

<style name="customfontstyle" parent="@android:style/TextAppearance.Small">
    <item name="android:fontFamily">@font/lobster</item> 
</style>

在您的情况下,您应该放置@font/前缀

<item name="android:fontFamily">@font/sans-serif-light</item>