为什么我的应用停止显示progressdialog?

时间:2017-10-07 06:24:31

标签: android android-recyclerview android-volley progressdialog android-glide

我已经创建了用于从服务器接收图像的应用程序,并且在我的应用程序中使用roller在recyclerview中显示滑动,我的应用程序有progressdialog并显示进度,当应用程序从服务器下载图像时滑动但经常我的应用程序停止正在进行而不是像下面的截图一样工作

image description

我必须关闭应用并再次打开它,这个问题的解决方案是什么,你可以在下面看到我的代码

 RecyclerView recycler_view;
 static final String url="";
 ArrayList<Image> images;
 GalleryAdapter mAdapter;
 ProgressDialog pd;
 public static Button btn_buy;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

recycler_view=(RecyclerView)findViewById(R.id.recycler_view);
btn_buy=(Button)findViewById(R.id.btn_buy);


pd=new ProgressDialog(this);
pd.setCancelable(false);

images=new ArrayList<>();
mAdapter=new GalleryAdapter(getApplicationContext(),images);


RecyclerView.LayoutManager mLayoutManager=new 
GridLayoutManager(getApplicationContext(),2);
recycler_view.setLayoutManager(mLayoutManager);

recycler_view.setAdapter(mAdapter);

ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);

recycler_view.addOnItemTouchListener(new 
GalleryAdapter.RecyclerTouchListener(getApplicationContext(),recycler_view, 
new GalleryAdapter.ClickListener() {
@Override
public void onClick(View view, int position) {

Bundle bundle=new Bundle();
bundle.putSerializable("images",images);
bundle.putInt("position",position);



FragmentTransaction ft=getFragmentManager().beginTransaction();

SlideshowDialogFragment newFragment=SlideshowDialogFragment.newInstance();
newFragment.setArguments(bundle);
newFragment.show(ft,"slideshow");


}

@Override
public void onLongClick(View view, int position) {

}
}));

btn_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,IABactivity.class);
startActivity(intent);

}
});


fetchImages();
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                     String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {

// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

      // permission was granted, yay! Do the
      // contacts-related task you need to do.
    } else {

      // permission denied, boo! Disable the
      // functionality that depends on this permission.
      Toast.makeText(MainActivity.this, "دسترسی به حافظه داخلی لغو شد!!!", Toast.LENGTH_LONG).show();
    }
    return;
  }

  // other 'case' lines to check for other
  // permissions this app might request
}
}



public void fetchImages()
{

pd.setMessage("Please wait ...");
pd.show();


StringRequest req = new StringRequest(url,
  new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
      Log.d("", response.toString());
      pd.hide();

      images.clear();
      try {
        JSONObject object = new JSONObject(response);
        JSONArray dataArray = object.getJSONArray("data");

        for (int i = 0; i < dataArray.length(); i++) {
          JSONObject dataObject = dataArray.getJSONObject(i);

          Image image = new Image();
          image.setName(dataObject.getString("name_client"));

          // JSONObject url = object.getJSONObject("url");
          image.setSmall(dataObject.getString("small"));
          image.setOriginal(dataObject.getString("orginal"));
          image.setTimestamp(dataObject.getString("timestamp"));

          images.add(image);

        }
      } catch (JSONException e) {
        e.printStackTrace();
      }

      mAdapter.notifyDataSetChanged();
    }
  }, new Response.ErrorListener() {
  @Override
  public void onErrorResponse(VolleyError error) {
    Log.e("", "Error: " + error.getMessage());
    pd.hide();
  }
});


AppController.getmInstance().addToRequsetQueue(req);

}
}

2 个答案:

答案 0 :(得分:2)

使用pd.dismiss()代替pd.hide()

答案 1 :(得分:2)

而不是pd.hide()使用pd.dismiss()