【Android运行时错误】Error inflating class CollapsingToolbarLayout

这篇具有很好参考价值的文章主要介绍了【Android运行时错误】Error inflating class CollapsingToolbarLayout。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

近期在调试代码时,突然遇到这样的一个错误,Error inflating class CollapsingToolbarLayout,这里记录一下。

刚开始并不知道问题所在,以为是自己的代码出问题了,因为毕竟报的错误是代码中的

java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.itbird.test/com.itbird.test.ui.activity.RandomActivity}: android.view.InflateException: 
Binary XML file line #22: Error inflating class android.support.design.widget.CollapsingToolbarLayout
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
  at android.app.ActivityThread.access$800(ActivityThread.java:151)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:135)
  at android.app.ActivityThread.main(ActivityThread.java:5254)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.CollapsingToolbarLayout
  at android.view.LayoutInflater.createView(LayoutInflater.java:633)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
  at com.itbird.test.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31)
  at android.app.Activity.performCreate(Activity.java:5990)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:135) 
  at android.app.ActivityThread.main(ActivityThread.java:5254) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.reflect.InvocationTargetException
  at java.lang.reflect.Constructor.newInstance(Native Method)
  at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
  at android.view.LayoutInflater.createView(LayoutInflater.java:607)
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) 
  at com.itbird.test.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31) 
  at android.app.Activity.performCreate(Activity.java:5990) 
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:135) 
  at android.app.ActivityThread.main(ActivityThread.java:5254) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Caused by: java.lang.NoSuchMethodError: No static method setLayoutDirection(Landroid/graphics/drawable/Drawable;I)V in class Landroid/support/v4/graphics/drawable/DrawableCompat; or its super classes (declaration of 'android.support.v4.graphics.drawable.DrawableCompat' appears in /data/app/com.itbird.test-1/base.apk)
  at android.support.design.widget.CollapsingToolbarLayout.setStatusBarScrim(CollapsingToolbarLayout.java:663)
  at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:197)
  at android.support.design.widget.CollapsingToolbarLayout.<init>(CollapsingToolbarLayout.java:132)
  at java.lang.reflect.Constructor.newInstance(Native Method) 
  at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
  at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
  at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
  at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276) 
  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136) 
  at com.itbird.test.ui.activity.RefreshableActivity.onCreate(RefreshableActivity.java:31) 
  at android.app.Activity.performCreate(Activity.java:5990) 
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
  at android.app.ActivityThread.access$800(ActivityThread.java:151) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:135) 
  at android.app.ActivityThread.main(ActivityThread.java:5254) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372)

自然而然,我就去查找自己的xml文件,到底有什么错误了

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clickable="true"
            app:contentScrim="@color/blue"
            app:statusBarScrim="@color/blue_dark"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            >

         //...省略

        </android.support.design.widget.CollapsingToolbarLayout>

看着xml代码很正常,那么我们还是返回问题本身,错误日志里面是说CollapsingToolbarLayout控件解析异常,那么是否是android.support包导入有问题?
最终,尝试在 gradle文件中,添加v4的支持,运行正常了。文章来源地址https://www.toymoban.com/news/detail-509748.html

compile ('com.android.support:support-v4:23.4.0'){
    force = true;
}

到了这里,关于【Android运行时错误】Error inflating class CollapsingToolbarLayout的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包