首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android SwipeRefreshLayout返回null

Android SwipeRefreshLayout返回null
EN

Stack Overflow用户
提问于 2020-10-07 18:39:40
回答 1查看 65关注 0票数 0

我想使用SwipeRefreshLayout替换数据,但当我这样做时

代码语言:javascript
复制
mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh);

mSwipeRefreshLayout为null。代码示例:

代码语言:javascript
复制
<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
 <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">

  </LinearLayout>

    </androidx.cardview.widget.CardView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


private SwipeRefreshLayout mSwipeRefreshLayout ;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Toast.makeText(SpisokActivity.this, "Обновление", Toast.LENGTH_SHORT).show();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });

所以,在mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh)之后,我得到了一个错误。我认为原因是mSwipeRefreshLayout有null。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-07 18:42:04

findViewById之前使用此命令

代码语言:javascript
复制
setContentView(R.layout.main)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64242338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档