2017-09-29 74 views
3

我的應用在android 26上崩潰,我的理解是它是由font-family和支持庫引起的,但我無法弄清楚原因。 在Android 8.0以下的設備上工作正常。字體家族導致安卓26上的NPE

Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class Button 
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
at android.support.v4.graphics.TypefaceCompatApi26Impl.addFontFromAssetManager(TypefaceCompatApi26Impl.java:150) 
at android.support.v4.graphics.TypefaceCompatApi26Impl.createFromFontFamilyFilesResourceEntry(TypefaceCompatApi26Impl.java:218) 
at android.support.v4.graphics.TypefaceCompat.createFromResourcesFamilyXml(TypefaceCompat.java:116) 
at android.support.v4.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:249) 
at android.support.v4.content.res.ResourcesCompat.loadFont(ResourcesCompat.java:213) 
at android.support.v4.content.res.ResourcesCompat.getFont(ResourcesCompat.java:206) 
at android.support.v7.widget.TintTypedArray.getFont(TintTypedArray.java:119) 
at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:208) 
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.AppCompatButton.<init>(AppCompatButton.java:77) 
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:67) 
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109) 
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:772) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
Caused by: java.lang.reflect.InvocationTargetException 
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v4.graphics.TypefaceCompatApi26Impl.addFontFromAssetManager(TypefaceCompatApi26Impl.java:145) 
... 42 more 
Caused by: java.lang.NullPointerException 
at android.graphics.FontFamily.nAddFontFromAssetManager(Native Method) 
at android.graphics.FontFamily.addFontFromAssetManager(FontFamily.java:149) 
... 44 more 

回答

3

通過只使用的xmlns:應用程序是爲Android V26

<font 
    app:font="@font/cuprum_regular" 
    app:fontStyle="normal" 
    app:fontWeight="400"/> 
1

工作從Fonts in XML docs

Support Library 26.0提供support於運行Android API version 14 and higher設備Fonts in XML功能 。

When you declare font families in XML layout through the support library, use the app namespace.

由於您的應用支持的API 14,所以你必須使用app命名空間,而不是android

0

我使用支持庫版本27.0.1我當我使用可下載的字體時,在Android Oreo上發生了完全相同的崩潰https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html
而不是使用可下載的字體,我只是從谷歌字體網站下載字體,並將它們放在res \ fonts目錄中。字體家族xml文件保持不變。 我測試過它,它在所有Android版本上都有。這是一個錯誤,或者我沒有正確設置可下載字體,即使我使用內置嚮導來添加新字體。