2016-08-01 2 views
0

У меня есть фрагмент, содержащий диалог прогресса, я внедрил код прогресса в другом классе, известном как DialogUtils, и когда я хочу показывать и отклонять диалог прогресса, он дает мне ошибку. Pls решает мою проблему.Как показать диалог выполнения в android

код: -

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: 
+0

Каков контекст вы предоставили в 'DialogUtils'? – Shaishav

+0

как вы вызываете 'postDealListingDatatoServer()' это из фонового потока или откуда? –

+0

if (mPdCommon! = Null && mPdCommon.isShowing()) { mPdCommon.dismiss(); } Отключить его перед увольнением – DKV

ответ

0

Сначала установите ориентацию активности в андроида манифеста

android:screenOrientation="portrait"

и перед вызовом

`"m_Dialog.dismiss()"` 

сделал чек на нуль и isShowing()

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

Смежные вопросы