當前在Fragment
我運行一個正常的Toast.makeText()
命令。該命令需要時間執行,因爲它在Volley
操作中。在Toast
確實出現的時間內,用戶有時間回到之前的Fragment
s。如果在這種情況下用戶返回到之前的Fragment
,則發生NullPointerException
。getActivity()的NullPointerException異常片段
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)
代碼:
Toast.makeText(
getSherlockActivity(),
"Sorry something went wrong. Please update again.",
Toast.LENGTH_LONG).show();
它在使用下面的方法(這就是所謂的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();
}
});
請幫助。
唯一的原因是getSherlockActivity返回null。 – Blackbelt
何時何地稱呼「吐司」? –
這怎麼解決?我也嘗試過使用getActivity()。 @blackbelt –