Android SwipeRefreshLayout 返回 null。

huangapple go评论72阅读模式
英文:

Android SwipeRefreshLayout returns null

问题

我想使用 SwipeRefreshLayout 来替换数据,但当我执行以下操作时:

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

mSwipeRefreshLayout 变成了 null。
代码示例:

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

英文:

I want to use a SwipeRefreshLayout for replace data,
but when i do

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

mSwipeRefreshLayout had null.
code example:

&lt;androidx.cardview.widget.CardView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;5dp&quot;&gt;
 &lt;androidx.cardview.widget.CardView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_margin=&quot;5dp&quot;&gt;

&lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_margin=&quot;10dp&quot;
            android:orientation=&quot;vertical&quot;&gt;

  &lt;/LinearLayout&gt;

    &lt;/androidx.cardview.widget.CardView&gt;
&lt;/androidx.swiperefreshlayout.widget.SwipeRefreshLayout&gt;


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, &quot;Обновление&quot;, Toast.LENGTH_SHORT).show();
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });

so, after
mSwipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.refresh) i get an error. I think that the reason is mSwipeRefreshLayout had null.

答案1

得分: 1

在使用 findViewById 之前,请使用以下代码:

setContentView(R.layout.main);
英文:

use this befor findViewById

setContentView(R.layout.main)

huangapple
  • 本文由 发表于 2020年10月7日 18:39:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64242338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定