简体   繁体   English

创建新项目符号时应用程序崩溃 [Andoid Studio]

[英]App crashes when I create a new bullet [Andoid Studio]

I'm trying to get my player to spawn a bullet every time I tap on the screen.我试图让我的玩家每次点击屏幕时都会产生一颗子弹。 However, every time I do so it crashes and gives me this error:但是,每次我这样做时它都会崩溃并给我这个错误:

E/AndroidRuntime: FATAL EXCEPTION: Thread-3
    Process: com.example.mgp2021, PID: 20750
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
        at com.example.mgp2021.Bullet.Render(Bullet.java:57)

Here is the bullet's script:这是子弹的脚本:

package com.example.spaceshooter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceView;


public class Bullet implements EntityBase{
    private boolean isDone = false;
    private Bitmap bullet = null, scaledbmp = null;

    public float xPos = 0;
    public float yPos = 0;

    private float yLimit = 0;
    //float imgRadius = bullet.getHeight() * 0.5f;

    @Override
    public boolean IsDone() {
        return isDone;
    }

    @Override
    public void SetIsDone(boolean _isDone) {
        isDone = _isDone;
    }

    @Override
    public void Init(SurfaceView _view) {
        bullet = BitmapFactory.decodeResource(_view.getResources(), R.drawable.bullet);
        //setting up values
        xPos = DraggablePlayer.Instance.xPos;
        yPos = DraggablePlayer.Instance.yPos;
        //yLimit = _view.getHeight() * 0.5f;
    }


    @Override
    public void Update(float _dt) {
        yPos -= _dt * 300.f;

        if(bullet == null)
        {
            Log.e("ERR","Failed to decode resource bullet");
        }
    }

    @Override
    public void Render(Canvas _canvas) {
        Matrix transform = new Matrix();
        transform.postTranslate(-bullet.getWidth()*0.5f, -bullet.getHeight()*0.5f);
        transform.postTranslate(xPos, yPos);
        _canvas.drawBitmap(bullet, transform, null);
    }

    @Override
    public boolean IsInit() {
        return bullet != null;
    }

    @Override
    public int GetRenderLayer() {
        return LayerConstants.BULLET_LAYER;
    }

    @Override
    public void SetRenderLayer(int _newLayer) {
    }

    @Override
    public ENTITY_TYPE GetEntityType() {
        return ENTITY_TYPE.ENT_BULLET;
    }

    public static Bullet Create() {
        Bullet result = new Bullet();
        EntityManager.Instance.AddEntity(result, ENTITY_TYPE.ENT_BULLET);
        return result;
    }
}

and here is the script for when I spawn the bullets:这是我产生子弹时的脚本:

public void Update(float _dt) {

        EntityManager.Instance.Update(_dt);

       if (TouchManager.Instance.IsDown())
       {
            Bullet.Create();
       }
    }

I'm really unsure of what's causing this error, any help is greatly appreciated我真的不确定是什么导致了这个错误,非常感谢任何帮助

The problem is that you call static Create method of your bullet class that return you an instance of Bullet but you never call (judging from code you posted) Init method that will actually read the contents of your R.drawable.bullet into a bitmap object. The problem is that you call static Create method of your bullet class that return you an instance of Bullet but you never call (judging from code you posted) Init method that will actually read the contents of your R.drawable.bullet into a bitmap object .

Answer: Make sure you call Init before Render .答:确保在Render之前调用Init


Compare creation of Bullet object to creation of other objects that work in your app (I'm talking about objects implementing EntityBase ).比较Bullet object 的创建与在您的应用程序中工作的其他对象的创建(我说的是实现EntityBase的对象)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM