如何在android中显示进度对话框

时间:2016-08-01 06:54:53

标签: android progressdialog

我有一个包含进度对话框的片段,我在另一个名为DialogUtils的类中实现了进度对话框代码,当我想显示和关闭进度对话框时,它给了我错误。请解决我的问题。

代码: -

public void postDealListingDatatoServer() {
    try {
        String json;
        // 3. build jsonObject
        final JSONObject jsonObject = new JSONObject();// making object of Jsons.
        jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
        jsonObject.put("pin", m_szEncryptedPassword);// put password
        jsonObject.put("recordcount", sz_RecordCount);// put record count
        jsonObject.put("lastcountvalue", sz_LastCount);// put last count
        System.out.println("Record Count:-" + sz_RecordCount);
        System.out.println("LastCount:-" + sz_LastCount);
        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();// convert Json object to string

        Log.i(TAG, "Server Request:-" + json);
        m_Dialog = DialogUtils.showProgressDialog(getActivity(), "Please wait while loading deals...");
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i(TAG, "Server Response:-" + response);
                m_Dialog.dismiss();
                try {
                    JSONArray posts = response.optJSONArray("dealList");// get Deal list in array from response
                    s_oDataset.clear();
                    for (int i = 0; i < posts.length(); i++) {// loop for counting deals from server
                        JSONObject post = posts.getJSONObject(i);// counting deal based on index
                        item = new CDealAppDatastorage();// creating object of DealAppdata storage
                        item.setM_szHeaderText(post.getString("dealname"));// get deal name from response
                        item.setM_szsubHeaderText(post.getString("dealcode"));// get dealcode from response
                        item.setM_szDealValue(post.getString("dealvalue"));// get deal value from response
                        item.setM_n_Image(m_n_FormImage[i]);//set Image Index wise(Dummy)
                        s_oDataset.add(item);// add all items in ArrayList

                    }


                    if (!s_oDataset.isEmpty()) {// condition if data in arraylist is not empty
                        m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
                        m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
                        m_NoInternetWarning.setVisibility(View.GONE);
                        mSwipeRefresh.setVisibility(View.VISIBLE);

                    } else {
                        m_ListView.removeFooterView(mFooter);// else Load buttonvisibility set to Gone
                    }

                    if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
                        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
                    } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
                        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
                    } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
                        CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Server error:-" + error);
                    m_Dialog.dismiss();
                if (error instanceof TimeoutError) {
                    CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection lost ! Please try again", getActivity());
                } else if (error instanceof NetworkError) {
                    CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No internet connection", getActivity());
                    mSwipeRefresh.setVisibility(View.GONE);
                    m_NoInternetWarning.setVisibility(View.VISIBLE);
                }

            }


});
            RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
            requestQueue.add(jsonObjectRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

Dialog Utils ::

    public class DialogUtils {

    public static ProgressDialog showProgressDialog(Context context, String message) {
        ProgressDialog m_Dialog = new ProgressDialog(context);
        m_Dialog.setMessage(message);
        m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        m_Dialog.setCancelable(false);
        m_Dialog.show();
        return m_Dialog;

    }

}

错误: -

java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{41fcfce0 V.E..... R......D 0,0-480,144} not attached to window manager
                                                                                  at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
                                                                                  at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
                                                                                  at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
                                                                                  at android.app.Dialog.dismissDialog(Dialog.java:360)
                                                                                  at android.app.Dialog.dismiss(Dialog.java:343)
                                                                                  at com.example.devui1.rewardapp.CDealAppListing$4.onResponse(CDealAppListing.java:243)
                                                                                  at com.example.devui1.rewardapp.CDealAppListing$4.onResponse(CDealAppListing.java:

1 个答案:

答案 0 :(得分:0)

首先在android manifest中设置活动方向

android:screenOrientation="portrait"

在致电之前

`"m_Dialog.dismiss()"`

检查了null和isShowing()

if(m_Dialog!=null && m_Dialog.isShowing()){ m_Dialog.dismiss(); }