【问题标题】:Android SwipeRefreshLayout returns nullAndroid SwipeRefreshLayout 返回 null
【发布时间】:2020-10-07 10:39:39
【问题描述】:

我想使用 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 有 null。

【问题讨论】:

    标签: java android swiperefreshlayout


    【解决方案1】:

    findViewById之前使用这个

    setContentView(R.layout.main)
    

    【讨论】:

    • 谢谢。错误是因为我写了 setContentView 不规则布局。
    猜你喜欢
    • 1970-01-01
    • 2012-06-22
    • 2012-05-28
    • 2014-12-05
    • 2021-07-07
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多