weixin_38295174 2018-10-25 09:16 采纳率: 0%
浏览 2356
已结题

Android 使用下拉刷新框架(SmartRefreshLayout),报错Error inflating class

我是一名小白,在学习安卓,刚接触到导入外部的框架,看到github上的项目SmartRefreshLayout,就想试用一下,但配置好后一直闪退,报错日志:Unable to start activity ComponentInfo{com.zhang.hp.muke_test/com.zhang.hp.muke_test.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.scwang.smartrefresh.layout.SmartRefreshLayout,

            xml文件写法:
 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/pull"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity写法:


        tv = findViewById(R.id.text);
        myrefresh = findViewById(R.id.pull);

        myrefresh.setOnRefreshListener(new OnRefreshListener() {

            //激活下拉刷新的时候调用的函数
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {

                tv.setText("下拉刷新了");
                myrefresh.finishRefresh();

            }
        });



        myrefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {

                tv.setText("上拉加载了");
                myrefresh.finishLoadMore();

            }
        });

        myrefresh.setEnableLoadMore(true);
        myrefresh.autoRefresh();

不知道哪里出错了,希望各位大神能指导一下,谢谢了!

  • 写回答

3条回答 默认 最新

  • KevinC0211 2021-07-27 16:51
    关注

    如果使用 AndroidX 先在 gradle.properties 中添加,两行都不能少噢~

    android.useAndroidX=true
    android.enableJetifier=true

    我试了下,因为少了这个

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用