Android:在附加的堆栈跟踪中获取资源但从未发布。有关避免资源泄漏的信息,请参阅java.io.Closeable

时间:2015-10-12 16:20:26

标签: java android android-studio

当我运行Android App时,出现以下错误:

10-12 16:46:44.719 2710-2719/? E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
10-12 16:46:44.719 2710-2719/? E/StrictMode: java.lang.Throwable: Explicit termination method 'end' not called
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at dalvik.system.CloseGuard.open(CloseGuard.java:184)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at java.util.zip.Inflater.<init>(Inflater.java:82)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okio.GzipSource.<init>(GzipSource.java:57)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.HttpEngine.initContentStream(HttpEngine.java:490)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:680)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:388)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:500)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.http.GoogleHttpClient.a(SourceFile:806)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.http.GoogleHttpClient.b(SourceFile:770)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:673)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:664)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.playlog.uploader.b.a(SourceFile:332)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.playlog.uploader.b.a(SourceFile:227)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.playlog.uploader.b.a(SourceFile:199)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.playlog.uploader.UploaderService.a(SourceFile:39)
10-12 16:46:44.719 2710-2719/? E/StrictMode:     at com.google.android.gms.gcm.af.run(SourceFile:130)

此错误意味着什么,我该如何解决?

1 个答案:

答案 0 :(得分:3)

你可以分享你的代码吗?您是否使用模拟器来运行应用程序或物理设备?

因为可能有很多原因:

  1. 你已经打开了一些东西但从未关闭它们。 Closable有一个close()方法,当您不再需要时,您必须调用该方法手动释放与该组件关联的已打开资源(例如在finally块中)。

  2. 此外,当AndroidManifest.xml出现问题时,会显示该错误消息。例如,当<activity>标记意外退出<application>

  3. 默认情况下,仿真器启用StrictMode,在实际设备上,可以通过以下代码启用它:

  4. 代码:

    StrictMode.setVmPolicy (new StrictMode.VmPolicy.Builder().detectAll().penaltyLog()
                                              .penaltyDeath().build());