I have this function in my onDestroy function and it’s causing a crash:
@Override
protected void onDestroy() {
Server.setPresence(false, CONSTANTS.USER.userId);
super.onDestroy();
}
Server.Java
private static final String TAG = "Cove Server";
private static final String PATH = "http://10.0.0.2:8001/data_connection";
private static HttpResponse response = null;
private static StringEntity se = null;
private static final int TIMEOUT = 30000;
private static HttpParams hParams = new BasicHttpParams();
private static HttpClient client;
private static HttpPost post = null;
public static String actionKey = null;
private static JSONObject sendRequest(JSONObject req) {
try {
HttpConnectionParams.setConnectionTimeout(hParams, TIMEOUT);
client = new DefaultHttpClient(hParams);
actionKey = req.getString("actionKey");
se = new StringEntity(req.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_ENCODING,
"application/json"));
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
post = new HttpPost(PATH);
post.setEntity(se);
Log.d(TAG, "http request is being sent");
response = client.execute(post);
Log.d(TAG, "http request was sent");
if (response != null
&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream in = response.getEntity().getContent();
String a = convertFromInputStream(in); // CALLS A FUNCTION THAT PARSES THE RESPONSE TO String
in.close();
return new JSONObject(a);
}
} catch (UnsupportedEncodingException e) {
Log.d(TAG, "encoding request to String entity faild!");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d(TAG, "executing the http POST didn't work");
e.printStackTrace();
} catch (IOException e) {
Log.d(TAG, "executing the http POST didn't work");
e.printStackTrace();
} catch (JSONException e) {
Log.d(TAG, "no ActionKey");
e.printStackTrace();
}
return null;
}
public static JSONObject setPresence(boolean isActive, String userId) {
JSONObject request = new JSONObject();
try {
request.put("actionKey", (isActive) ? "UserPresenceActive"
: "UserPresenceInactive");
request.put("userId", userId);
return sendRequest(request);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
logcat
06-28 12:59:11.970: E/AndroidRuntime(19806): FATAL EXCEPTION: main
06-28 12:59:11.970: E/AndroidRuntime(19806): java.lang.RuntimeException: Unable to destroy activity {com.yishai/com.thepoosh.MyActivity}: java.lang.NullPointerException
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3124)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3142)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.ActivityThread.access$1200(ActivityThread.java:127)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1192)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.os.Looper.loop(Looper.java:137)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.ActivityThread.main(ActivityThread.java:4507)
06-28 12:59:11.970: E/AndroidRuntime(19806): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 12:59:11.970: E/AndroidRuntime(19806): at java.lang.reflect.Method.invoke(Method.java:511)
06-28 12:59:11.970: E/AndroidRuntime(19806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-28 12:59:11.970: E/AndroidRuntime(19806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-28 12:59:11.970: E/AndroidRuntime(19806): at dalvik.system.NativeStart.main(Native Method)
06-28 12:59:11.970: E/AndroidRuntime(19806): Caused by: java.lang.NullPointerException
06-28 12:59:11.970: E/AndroidRuntime(19806): at com.thepoosh.MyActivity.onDestroy(MyActivity.java:340)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.Activity.performDestroy(Activity.java:4629)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1082)
06-28 12:59:11.970: E/AndroidRuntime(19806): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3111)
06-28 12:59:11.970: E/AndroidRuntime(19806): ... 11 more
I’m suspecting that since the server is not sending back a response and the Server.setPresence() doesn’t return and therefore the app goes into ANR.
Is that correct? What should I do to fix this? Is there a different reason for the crash?
looks on
onDestroy is last function is stacktraceso call not went to Server.setPresence yet .. so it looksUSERinCONSTANTS.USER.userIdmay be only null at that line. so put a check to test out that.