Android中的Cusum退出对话框

时间:2018-10-10 02:06:17

标签: java android android-studio alertdialog customdialog

我想创建一个自定义退出对话框,其中包含从服务器获取的三个图像。如果我单击后退按钮,将显示对话框。向我显示了对话框,但未显示从服务器获取的图像,而是显示了默认图像。图片网址已成功从服务器获取。如果我将该网址放在imageView中,则会生成异常:

  

AndroidRuntime:致命异常:主要                                                                                        流程:com.example.madnanijaz.labassignment1,PID:2967                                                                                        java.lang.NullPointerException:参数不能为null                                                                                            在com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:27)                                                                                            在com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:22)                                                                                            在com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:383)                                                                                            在com.example.madnanijaz.labassignment1.MainActivity $ 7.onResponse(MainActivity.java:181)                                                                                            在com.example.madnanijaz.labassignment1.MainActivity $ 7.onResponse(MainActivity.java:170)

我的后按按钮的代码是:

@Override
public void onBackPressed() {

    //Fetching Images(Adds from server)
    fetchingAddsFromServer();

    //CustomAlertDialog

    final AlertDialog.Builder builder=new 
   AlertDialog.Builder(MainActivity.this);
    View view= 
  LayoutInflater.from(MainActivity.this).inflate(R.layout.row_layout,null);

    builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            finish();
        }
    });
    builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            dialogInterface.dismiss();
        }
    });

    builder.setView(view);
    builder.show();

   // AlertDialog alertDialog=builder.create();
    //alertDialog.show();

}

  private void fetchingAddsFromServer() {

    StringRequest request= new StringRequest(URL, new 
    Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject object= new JSONObject(response);
                JSONArray jsonArray=object.getJSONArray("slots");

                    JSONObject jsonObject=jsonArray.getJSONObject(1);
                Toast.makeText(MainActivity.this, "Image Url 
     is:"+jsonObject.getString("imgurl"), Toast.LENGTH_LONG).show();
               // String ImgURI=jsonObject.getString("imgurl");

  Glide.with(getApplicationContext()).load(jsonObject.getString("imgurl")).into(c 
       losingDialogImageTwo);

                //}

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

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    RequestQueue queue= Volley.newRequestQueue(MainActivity.this);
    queue.add(request);

我认为这是自定义布局的错误。我在主屏幕中有一个ImageView。如果我使用Glide可以正常工作,但是如果使用Glide进行对话框的自定义布局,则会出现此错误。

1 个答案:

答案 0 :(得分:0)

以下错误:

  

AndroidRuntime:致命异常:主要                                                                                        流程:com.example.madnanijaz.labassignment1,PID:2967                                                                                        java.lang.NullPointerException:参数不能为null                                                                                            在com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:27)                                                                                            在com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:22)                                                                                            在com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:383)                                                                                            在com.example.madnanijaz.labassignment1.MainActivity $ 7.onResponse(MainActivity.java:181)                                                                                            在com.example.madnanijaz.labassignment1.MainActivity $ 7.onResponse(MainActivity.java:170)

发生这种情况是因为您尝试使用以下方法将图像加载到closingDialogImageTwo

Glide.with(getApplicationContext()).load(jsonObject.getString("imgurl")).into(c losingDialogImageTwo);

,但您尚未初始化视图。由于图像在对话框的自定义视图中,因此您需要在扩展视图后对其进行初始化。在以下代码之后:

View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.row_layout,null);

与此:

ImageView closingDialogImageTwo = view.findViewById(R.id.your_image_id);

您仅需要在fetchingAddsFromServer()方法完成获取图像后才加载图像,因为它的异步行为。

总而言之,您需要像这样在fetchingAddsFromServer内移动AlertDialog创建:

private void fetchingAddsFromServer() {
    StringRequest request= new StringRequest(URL, new 
    Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

          ...  
          // assuming you got the image.
          String imageUrl = jsonObject.getString("imgurl");

          showDialogWithImage(imageUrl);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

    RequestQueue queue= Volley.newRequestQueue(MainActivity.this);
    queue.add(request);
}


private void showDialogWithImage(String imageUrl) {
  final AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
  View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.row_layout,null);

  ImageView closingDialogImageTwo = view.findViewById(R.id.your_image_id);

  Glide.with(getApplicationContext())
       .load(imageUrl).into(closingDialogImageTwo);

  builder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            finish();
        }
    });
    builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            dialogInterface.dismiss();
        }
    });

    builder.setView(view);
    builder.show();
}