我想使用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。
发布于 2020-10-07 18:42:04
在findViewById之前使用此命令
setContentView(R.layout.main)https://stackoverflow.com/questions/64242338
复制相似问题