为什么添加和删除片段会导致内存泄漏?

时间:2015-12-30 03:16:05

标签: java android android-fragments

我对Android dev相当新,所以任何帮助都非常感谢! 我收到此错误

  

12-30 08:14:32.391 1726-1741 /? E / StrictMode:在附加堆栈跟踪处获取资源但从未释放。有关避免资源泄漏的信息,请参阅java.io.Closeable。       java.lang.Throwable:未调用显式终止方法'end'               在dalvik.system.CloseGuard.open(CloseGuard.java:180)               在java.util.zip.Inflater。(Inflater.java:82)               在com.android.okhttp.okio.GzipSource。(GzipSource.java:62)               在com.android.okhttp.internal.http.HttpEngine.unzip(HttpEngine.java:645)               在com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:827)               在com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:439)               在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)               在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)               在com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)               at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java)               在com.google.android.gms.http.GoogleHttpClient.a(SourceFile:811)               在com.google.android.gms.http.GoogleHttpClient.a(SourceFile:776)               在com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:676)               在com.google.android.gms.http.GoogleHttpClient.execute(SourceFile:660)               在com.google.android.gms.auth.be.j.a(SourceFile:220)               在com.google.android.gms.auth.be.appcert.a.a(SourceFile:263)               在com.google.android.gms.auth.be.appcert.a.a(SourceFile:132)               在com.google.android.gms.auth.be.appcert.b.a(SourceFile:43)               在com.google.android.gms.auth.b.b.a(SourceFile:62)               在com.google.android.gms.auth.b.a.a(SourceFile:120)               在com.google.android.gms.auth.b.a.a(SourceFile:61)               在com.google.android.gms.auth.be.cron.AuthCronService.a(SourceFile:44)               在com.google.android.gms.gcm.al.run(SourceFile:135)

以及导致错误的部分代码

 gameLayout.setOnTouchListener(
            new FrameLayout.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    //add bullet upon click
                    bullet_fragment b_fragment = new bullet_fragment();
                    fragmentTransaction.add(R.id.game, b_fragment).commit();

                    //bullet moves around screen (some code here)

                    // removes bullet
                    fragmentTransaction.remove(b_fragment).commit();
                    return true;
                }
          g  }
    );

因此每次触摸屏幕时都会生成一个片段,因此屏幕上可能存在多个相同的片段,这是没有问题的 我尝试了一下,发现remove()不一定会将片段移动到GC,但该人没有说明如何彻底销毁它。那怎么办呢?还是有其他原因发生错误?

片段的代码

public class bullet_fragment extends Fragment {
View bulletView;

ImageView bulletObject;




@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    bulletView = inflater.inflate(R.layout.bullet_layout, container, true);
    bulletObject = (ImageView) bulletView.findViewById(R.id.bullet);

    return bulletView;

}
public void shoot(int x, int y){
    FrameLayout.LayoutParams b_params = new FrameLayout.LayoutParams(bulletObject.getLayoutParams());


    final int B_WIDTH= (int) (bulletObject.getWidth() * 0.5);
    final int B_HEIGHT= (int) (bulletObject.getHeight() * 0.5);
    b_params.setMargins(x - B_WIDTH,y - B_HEIGHT,0,0);
    bulletObject.setLayoutParams(b_params);
}

}

1 个答案:

答案 0 :(得分:0)

堆栈跟踪中的问题显然是由okhttp引起的,但它可能不一定是okhttp的错误。事实上,这发生在作为Google服务一部分的gms内部,这表明您可能与此问题无关。您确定此日志是针对您的流程而不是某些系统流程吗?

okhttp参考

毕加索参考(作为图书馆可能出错的一个例子)