吐司没有显示出来

时间:2017-04-05 09:20:53

标签: android android-fragments android-volley android-alertdialog

  

我正在使用凌空进行网络通话

凌空请求中的吐司未显示。我不知道如何从片段中获取警报对话框中的上下文。所以请解决我如何在排球响应中打印吐司。

public class TiffinMenuFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog.Builder alertdialog = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = (LayoutInflater) getActivity().getBaseContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.fragment_tiffinlayout, null);
    alertdialog.setView(v);
    alertdialog.setCancelable(false);


    final AlertDialog dialog = alertdialog.create();

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            String type=selectmenu.getSelectedItem().toString();
            Log.v("java1",type);


            String url_menu=UrlString.url_string;
            url_menu = url_menu.replace(" ", "%20");
            Log.v("url",url_menu);
            final StringRequest stringRequest=new StringRequest(Request.Method.GET, url_menu, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.v("Response",response);
                    JSONObject obj= null;
                    try {
                        obj = new JSONObject(response);
                        String objResponse=obj.getString("success");
                        Log.v("Response1",objResponse);
                        Toast.makeText(view.getContext(),"Task is Completed",Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                Log.v("type",error.getMessage());
                }
            });
            MySingleton.getInstance(getActivity().getApplicationContext()).addToRequestque(stringRequest);
            dialog.dismiss();

        }
    });
    cancle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();

        }
    });




    return dialog;

}
}

6 个答案:

答案 0 :(得分:0)

只是做

Toast.makeText(getActivity(),"Text!",Toast.LENGTH_SHORT).show();

答案 1 :(得分:0)

您在Toast中使用不正确的使用情况

 Toast.makeText(getActivity(),"Task is Completed",Toast.LENGTH_LONG).show();

答案 2 :(得分:0)

                Toast.makeText(dialog.getContext(),"Task is Completed",Toast.LENGTH_LONG).show();

使用此功能。

答案 3 :(得分:0)

使用:

Toast.makeText(getActivity().getApplicationContext(),"Task is
Completed",Toast.LENGTH_LONG).show();

答案 4 :(得分:0)

  Toast.makeText(dialog.getActivity(),"Task is Completed",Toast.LENGTH_LONG).show();

答案 5 :(得分:-1)

您无法从后台线程

触摸UI

要显示吐司,请使用此代码

activity.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
    }
});

或请求失败且未调用 onResponse 函数。