英文:
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:
<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);
}
});
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论