2017-10-10 5 views
1

Chaque fois que je montre un Toast, l'application se bloque.Toast lance ArrayIndexOutOfBoundsException avec appcompat v26 lors de l'utilisation de l'attribut fontFamily dans le thème

L'application fonctionne très bien si j'utilise une ancienne version de la bibliothèque AppCompat ou supprimer fontFamily du 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 
} 

Dépendance:

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> 

Trace de pile:

causés par: java.lang.ArrayIndexOutOfBoundsException: longueur = 16; index = 233 à android.content.res.StringBlock.get (StringBlock.java:65) à android.content.res.XmlBloc $ Parser.getPooledString (XmlBlock.java:458) at 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) à androïde .support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle (AppCompatTextHelper.java:215) à android.support.v7.widget.AppCompatTextHelper.loadFromAttributes (AppCompatTextHelper.java:152) à android.support.v7.widget.AppCompatTextHelp erV17.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) at android.app.Activity.performCreate (Activity.java:6679) à android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1 119) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2618) à android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2726) at android.app.ActivityThread.-wrap12 (ActivityThread.java) à l'adresse android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1477) at android.os.Handler.dispatchMessage (Handler.java:102) à android.os.Looper.loop (Looper.java:154) à android.app.ActivityThread.main (ActivityThread.java:6126) à 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)

Existe-t-il une alternative pour que je puisse utiliser l'attribut fontFamily dans le thème en utilisant la dernière version de la bibliothèque AppCompat?

Répondre

6

Ajouter la police dans le thème comme dessous-

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

Il a travaillé. Génial! –

+0

Merci pour la solution! Une question: Pourquoi est-ce 'android: Widget.Holo.Button' au lieu de' android: Widget.Button'? Serait-ce toujours la même chose si mon thème de base utilise 'Base.Theme.AppCompat.Light.DarkActionBar'? – Aba

+1

@Aba Non, le style 'Holo' ne créera pas le même design. J'ai édité la réponse. –

4

Selon le Guide des développeurs Android pour fonts in xml

Ajout de polices au style

Ouvrez le styles.xml, et régler la fontFamily attribue au fichier de police auquel vous voulez accéder.

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

Dans votre cas, vous devez mettre le préfixe @font/

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

Merci d'avoir essayé de nous aider. –