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 Fragment
s. 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.