Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toast throws ArrayIndexOutOfBoundsException with appcompat v26 when using fontFamily attribute in theme

Whenever I show a Toast, the app crashes.

The app works fine if I use older version of AppCompat library or remove fontFamily from the style.

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
}

Dependency:

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>

Stack Trace:

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

Is there any alternative so that I can use the fontFamily attribute in theme using the latest version of AppCompat library?

like image 389
Nabin Bhandari Avatar asked Dec 05 '22 13:12

Nabin Bhandari


2 Answers

Add font in Theme like below-

    <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>
like image 141
Gaurav Avatar answered Dec 08 '22 03:12

Gaurav


According to the Android Developer Guide for fonts in xml

Adding fonts to style

Open the styles.xml, and set the fontFamily attribute to the font file you want to access.

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

In your case you should put the @font/ prefix

<item name="android:fontFamily">@font/sans-serif-light</item>
like image 33
pleft Avatar answered Dec 08 '22 03:12

pleft