我的onDestroy
函数中有这个函数,导致崩溃:
@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
我怀疑由于服务器没有发回回复而且Server.setPresence()
没有返回,因此应用程序进入ANR。
这是对的吗?我该怎么做才能解决这个问题?坠机原因有什么不同吗?
答案 0 :(得分:1)
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)
在onDestroy is last function is stacktrace
上查看,因此调用尚未转到Server.setPresence ..因此USER
中的CONSTANTS.USER.userId
看起来可能只在该行处为空。所以请检查一下。
@Override
protected void onDestroy() {
if(null==CONSTANTS.USER){
Server.setPresence(false, CONSTANTS.USER.userId);
}else{
Toast.makeText(this,"is null",Toast.LENGHT_LONG).show();
}
super.onDestroy();
}