1

Currently in a Fragment I run a normal Toast.makeText() command. The command takes time to execute as it is inside a Volley operation. During the time that the Toast actually does appear the user has time to go back to previous Fragments. If in that situation the user goes back to the previous Fragment, a NullPointerException occurs.

11-28 13:53:53.965: E/AndroidRuntime(22654): FATAL EXCEPTION: main
11-28 13:53:53.965: E/AndroidRuntime(22654): Process: com.developmentcheck.dcforpublic, PID: 22654
11-28 13:53:53.965: E/AndroidRuntime(22654): java.lang.NullPointerException
11-28 13:53:53.965: E/AndroidRuntime(22654):    at android.widget.Toast.<init>(Toast.java:93)
11-28 13:53:53.965: E/AndroidRuntime(22654):    at android.widget.Toast.makeText(Toast.java:241)

The code :

Toast.makeText(
        getSherlockActivity(),
        "Sorry something went wrong. Please update again.",
        Toast.LENGTH_LONG).show();

It is used in the following method(which is called in the onActivityCreated):

public void castVote(final String id, int vote_state,
        final String situations) throws JSONException {

JsonObjectRequest jsrq = new JsonObjectRequest(Request.Method.POST,
        url, json, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                // TODO Auto-generated method stub
                Log.i("JSON_RESPONSE_VOTE", response.toString());
                Toast.makeText(getSherlockActivity(), "Your vote was sent",
                        Toast.LENGTH_LONG).show();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getSherlockActivity(),
                        "Sorry something went wrong. Please update again.",
                        Toast.LENGTH_LONG).show();

            }
        });

Please help.

Rakeeb Rajbhandari
  • 5,043
  • 6
  • 43
  • 74

2 Answers2

3

Create a member variable mPendingToastText in your fragment class. Then try this:

@Override
public void onErrorResponse(VolleyError error) {
    // TODO Auto-generated method stub
    if (getSherlockActivity() != null) {
        Toast.makeText(
            getSherlockActivity(),
            "Sorry something went wrong. Please update again.",
            Toast.LENGTH_LONG).show();
    } else {
         mPendingToastText = "Sorry something went wrong. Please update again.";
    }

}

Now override a onAttach() event to your fragment like this:

@Override
public void onAttach(Activity activity) {
    if (!TextUtils.isEmpty(mPendingToastText) {
                Toast.makeText(
                    activity,
                    mPendingToastText,
                    Toast.LENGTH_LONG).show();
                mPendingToastText = "";
    }
    super.onAttach(activity);
}

This code is not a good solution but it should give you an idea of what is happening. Ideally you should have some kind of method that centralizes your toasts. For example you could register a broadcast receiver in your activity and send all your toasts to be processed by it.

Hope it helps.

Ricardo
  • 7,785
  • 8
  • 40
  • 60
  • rather than using the `getActivity()` or `getSherlockActivity()` ... I in the `onAttach()` declared ... `this.activity = activity;` ... So while calling the Toast ... I declare the `Toast.makeText(activity, ...)` , which works. You could make this as an edit ... I will delete the comment ...and thanks :) the idea came from you. – Rakeeb Rajbhandari Nov 28 '13 at 09:09
  • Good! I'm glad it worked for you. I think you can leave your comment there. It is a good idea to do what you said but just remember to set `this.activity = null` on `onDetach()` as well to avoid other exceptions. :) – Ricardo Nov 28 '13 at 09:10
1

This simple way:

  1. First declare context variable:
    private Context context;
  2. In onCreateView():
    context = container.getContext();
  3. Use it for Toast:
    Toast.makeText(context, "Your vote was sent", Toast.LENGTH_LONG).show();

This is get context from container as ViewGroup, so Toast can be shown.

Hope it helps.

misbahm3
  • 101
  • 5