在附加的堆栈跟踪中获取资源但从未发布错误

时间:2018-06-09 07:13:19

标签: java android

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

  

“资源是在附加的堆栈跟踪中获取但从未发布的”

enter image description here

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();
    }
}

1 个答案:

答案 0 :(得分:0)

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

请检查更改后的Texture和您将从组件中释放的图形 - &gt;当你想改变某事时重新打开。

PS Sry for long text但它是我想要描述的一般理论问题。无论如何,修复是一个你将获得的oneliner。