Raghav
Raghav

Reputation: 137

Getting error in Progress Dialog in android?

I have a MainActivity which adds fragment "A",In fragment"A" I am sending some server request using volley.I had made a class known as DialogUtil which contain progress Dialog implementation.Problem is that when I launch app it shows error in Progress dialog implementation in Fragment "A".That is

java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{42759d68 V.E..... R......D 0,0-456,144} not attached to window manager and becomes force close.

DialogUtil class code:-

   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;
    }
}

Progress dialog implementation in Fragment "A"

m_Dialog = DialogUtils.showProgressDialog(getContext(), "Loading...");

        final String m_DealListingURL = "http://202.131.1.132:8080/ireward/rest/json/metallica/getDealListInJSON";
        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);
                if (m_Dialog != null && m_Dialog.isShowing()) {
                    m_Dialog.dismiss();
                }

Upvotes: 0

Views: 505

Answers (4)

Rahul Sharma
Rahul Sharma

Reputation: 19

you should use getActivity() instead of getContext().

Upvotes: 1

Ramit
Ramit

Reputation: 426

I think problem in calling code in fragment life cycle. Please make sure you are calling dialog after onActivityCreated.

Upvotes: 0

Rahul Sharma
Rahul Sharma

Reputation: 13593

Try this:

DialogUtils.showProgressDialog(getActivity(), "Loading...");

Use getActivity() instead of getContext()

Upvotes: 4

Pasi Matalam&#228;ki
Pasi Matalam&#228;ki

Reputation: 1853

Are you calling the method asynchronously?

If you are, the chances are, user has exited the context(fragment, activity or application) by the time the code runs that is creating the dialog, which is bad in multiple ways.

You can solve this problem, if you're running a AsyncTask for example, by calling in onDestroy the AsyncTasks cancel method, but the specific usecase doesn't come clear from the code you have provided.

Upvotes: 0

Related Questions