【问题标题】:A resource was acquired at attached stack trace but never released error在附加的堆栈跟踪中获取了资源但从未释放错误
【发布时间】:2018-06-09 07:13:19
【问题描述】:

运行时错误使我的应用程序崩溃。它在制作矩形数组列表后开始。我该如何解决?

“在附加的堆栈跟踪中获取了资源,但从未释放”

package com.mygdx.game;        

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;

import org.w3c.dom.css.Rect;
import java.util.ArrayList;
import java.util.Random;

public class coingame extends ApplicationAdapter {
    SpriteBatch batch;
    int i=0;
    int bomb2=0;
    ArrayList<Integer> bombposition2=new ArrayList<Integer>();
    Texture bomb;
    int d=0;
    Rectangle rectangle3;
    int coin2=0;
    int k=0;
    int n=0;
    Texture coin;
    ArrayList<Integer> bombposition=new ArrayList<Integer>();
    ArrayList<Rectangle> rectangle=new ArrayList<Rectangle>();
    float velocity=0;
    float manposition=0;
    ArrayList<Integer> coinposition=new ArrayList<Integer>();
    ArrayList<Rectangle> rectangle2=new ArrayList<Rectangle>();
    ArrayList<Integer> coinposition2=new ArrayList<Integer>();
    float gravity=0.2f;
    int c=0;
    Texture a;
    Texture[] b;

    @Override
    public void create () {
        batch = new SpriteBatch();
        bomb=new Texture("bomb.png");
        rectangle3=new Rectangle();
        a=new Texture("bg.png");
        coin =new Texture("coin.png");
        b=new Texture[4];
        b[0]=new Texture("frame-1.png");
        b[1]=new Texture("frame-2.png");
        b[2]=new Texture("frame-3.png");
        b[3]=new Texture("frame-4.png");
        manposition=Gdx.graphics.getHeight();
    }

    @Override
    public void render () {
        batch.begin();
        batch.draw(a, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        if(coin2<100)
        {
            coin2=coin2+1;
        }
        else
        {
            coin2=0;
            n=new Random().nextInt(Gdx.graphics.getHeight());
            coinposition.add(Gdx.graphics.getWidth());
            coinposition2.add(k);
        }
        if(bomb2<200)
        {
            bomb2=bomb2+1;
        }
        else
        {
            bomb2=0;
            k=new Random().nextInt(Gdx.graphics.getHeight());
            bombposition.add(Gdx.graphics.getWidth());
            bombposition2.add(k);
        }
        rectangle.clear();
        for(i=0;i<coinposition.size();i++)
        {
            batch.draw(coin,coinposition.get(i),coinposition2.get(i));
            coinposition.set(i,coinposition.get(i)-4);
            rectangle.add(new Rectangle(coinposition.get(i),coinposition2.get(i),coin.getWidth(),coin.getHeight()));
        }
        rectangle2.clear();
        for(i=0;i<bombposition.size();i++)
        {
            batch.draw(bomb,bombposition.get(i),bombposition2.get(i));
            bombposition.set(i,bombposition.get(i)-8);
            //rectangle2.add(new Rectangle(bombposition.get(i),bombposition2.get(i),bomb.getHeight(),bomb.getWidth()));
        }
        if(Gdx.input.justTouched()==true)
        {
            velocity=-10;
            gravity=0;
        }
        if(d<8)
        {
            d=d+1;
        }
        else {
            d=0;
            if (c < 3) {
                c = c + 1;
            } else {
                c = 0;
            }
        }
        velocity=velocity+gravity;
        gravity=gravity+0.2f;
        manposition=manposition-velocity;
        if(manposition<0||manposition==0)
        {
            manposition=0;
        }
        batch.draw(b[c],Gdx.graphics.getWidth()/2-b[c].getWidth()/2,manposition);
        //rectangle3=new Rectangle(Gdx.graphics.getWidth()/2-b[c].getWidth()/2,manposition,b[c].getWidth(),b[c].getHeight());
        /*batch.draw(b[1],Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
        batch.draw(b[2],Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
        batch.draw(b[3],Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);*/
        for(i=0;i<rectangle.size();i++)
        {
            if(Intersector.overlaps(rectangle3,rectangle2.get(i)))
            {
                Gdx.app.log("this is a","this is a");
            }
        }
        batch.end();
    }

    @Override

    public void dispose () {
        batch.dispose();
    }
}

【问题讨论】:

    标签: java android


    【解决方案1】:

    这是打开 closeable 的资源(文件/...)的正常行为,您只需打开它,从不关闭它。 当您在方法中读取/写入文件并考虑处理方法后的 GC 时,通常会发生这种情况。您需要自行关闭此类资源 - 否则它们将被锁定以供其他线程/程序/重新运行/...

    所以检查Texture´s 和您的图形,它们将在更改后从组件中释放 -> 仅在您想要更改某事时重新打开。


    PS 很抱歉,对于长文本,但这是一个我想描述的一般理论问题。无论如何,修复是一个单线器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      相关资源
      最近更新 更多