我尝试在回收器视图上更改设备的方向,但是在显示进度对话框时,它总是崩溃。

如何解决呢?

这是我的代码:

私有类LoadOrdersListAgent扩展了AsyncTask {

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(OrdersActivity.this);
        ordersList = new ArrayList<>();
        rvor = findViewById(R.id.recycler_view_orders_agent);
        emptytv = findViewById(R.id.empty_view_orders_agent);
        emptytv.setVisibility(View.GONE);
        rvor.setHasFixedSize(true);
        rvor.setLayoutManager(new LinearLayoutManager(OrdersActivity.this));
        rvor.setItemAnimator(new DefaultItemAnimator());

        dialog.setMessage("Loading....");
        dialog.show();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        final OrdersAdapter adapter = new OrdersAdapter(getApplicationContext(), ordersList);
        rvor.setAdapter(adapter);
        rvor.setLayoutManager(new LinearLayoutManager(OrdersActivity.this));
        srl.setRefreshing(false);

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (ordersList.isEmpty()) {
            Log.d("TESTING.....", "LIST OF ORDERS ----->" + ordersList);
            rvor.setVisibility(View.GONE);
            srl.setVisibility(View.GONE);
            emptytv.setVisibility(View.VISIBLE);
        } else {
            rvor.setVisibility(View.VISIBLE);
            srl.setVisibility(View.VISIBLE);
            emptytv.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected Void doInBackground(Void... voids) {
        ordersList = OrdersApi.getOrders(url, key);
        return null;
    }
}

private void swipeOrderLayout() {
    srl = findViewById(R.id.swipe);

    srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            if (new CheckNetworkUtil(OrdersActivity.this).isNetworkAvailable()) {
                new LoadOrdersListAgent().execute();


// new LoadOrdersListAdmin()。execute();
                }其他
                    Toast.makeText(OrdersActivity.this,“没有Internet连接!”,Toast.LENGTH_SHORT).show();
                srl.setRefreshing(false);
            }
        });
    }

最佳答案

我在完成/销毁活动而不取消进度对话时收到此错误。

解决方案使用dialog.dismiss();在销毁或暂停活动之前关闭进度对话框

在您的情况下,删除if条件,然后在postExecute方法中调用dialog.dismiss();

07-26 07:07