Android SwipeRefreshLayout returns 空

Android SwipeRefreshLayout returns null

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

mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh);

mSwipeRefreshLayout 为空。 代码示例:

<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 为空。

findViewById

之前使用这个
setContentView(R.layout.main)