使用chrisjenx CalligraphyLayoutInflater时出现InflateException,android.view.InflateException

7

嗨,我们的项目使用了calligraphy库,一直工作得很好。但现在在其中一个活动中添加它后,发生了以下堆栈跟踪导致崩溃:

01-23 02:25:32.178 3654-3654/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 3654
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.activities.MultipleActivity}: android.view.InflateException: Binary XML file line #98: Error inflating class com.example.components.WLayout
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #98: Error inflating class com.example.components.WLayout
    at android.view.LayoutInflater.createView(LayoutInflater.java:621)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.createCustomViewInternal(CalligraphyLayoutInflater.java:211)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.access$000(CalligraphyLayoutInflater.java:20)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$PrivateWrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:302)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.example.activities.MultipleActivity.onCreate(MultipleActivity.java:62)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
    at android.app.ActivityThread.access$800(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at android.view.LayoutInflater.createView(LayoutInflater.java:595)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.createCustomViewInternal(CalligraphyLayoutInflater.java:211) 
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.access$000(CalligraphyLayoutInflater.java:20) 
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater$PrivateWrapperFactory2.onCreateView(CalligraphyLayoutInflater.java:302) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:756) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
    at com.example.activities.MultipleActivity.onCreate(MultipleActivity.java:62) 
    at android.app.Activity.performCreate(Activity.java:5231) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
    at android.app.ActivityThread.access$800(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5017) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class TextView
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:840)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at uk.co.chrisjenx.calligraphy.CalligraphyLayoutInflater.inflate(CalligraphyLayoutInflater.java:60)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)

我查看了一些答案,但没有看到任何人遇到TextView的问题。

在我的活动中,我将它附加到书法如下:

@Override
protected void attachBaseContext(Context newBase) {
  super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
活动主题如下:
    <style name="AppTheme.Compat.ActionBar.Dark.NoBackground" parent="AppTheme.Compat.ActionBar.Dark">
        <item name="android:windowBackground">@null</item>
        <item name="android:textColor">@color/alert_title</item>
        <item name="android:textColorPrimary">@color/gray400</item>
        <item name="android:colorAccent">@color/alert_button</item>
    </style>

我注意到 studio 给出了关于 android:colorAccent 的警告,并将其替换为 "colorAccent",虽然警告已消除,但崩溃仍存在。有什么线索吗?我在这里做错了什么吗?

添加 multipleActivity.class 的 Java 代码。 - W4R10CK
@amIT,有什么进展了吗? - Andrea Thacker
能够找到解决方案并将其发布为答案@David Argyle Thacker。 - amIT
2个回答

1

最终找到了问题所在,由于崩溃日志显示问题发生在Textview中,经过验证,我发现是因为我给Textview设置了背景图像,而且这是一个SVG图像,并且我使用了AppCompact库(v23)。通过"app:srcCompat"或"setImageResource()"设置矢量可绘制是使用矢量可绘制的可靠方法。我阅读了这篇博客文章https://android-developers.googleblog.com/2016/02/android-support-library-232.html,看到以前支持的Textview现在已经被删除了,这意味着它不稳定,容易出错。

我移除了Textview上的矢量可绘制,并在Textview下方添加了另一个ImageView,使用"src:compact"添加了矢量可绘制,这样它们在视觉上仍然相同,即带有背景的Textview。

TLDR:不要为TextView使用setbackground,这容易出现错误(三星),尤其是如果使用矢量可绘制。

如果应用大小不是问题,您也可以使用PNG/JPG资源,避免在布局中创建额外的ImageView。

0

针对Android Studio 3.0.1

我遇到了同样的问题,最终通过打开gradle.properties并添加以下行来解决:

android.enableAapt2=false

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接