应用程序在启动主要活动时崩溃

时间:2016-02-25 09:40:05

标签: java android android-studio logcat

当我启动主要活动时,我的应用程序因 NullPointerExeption 而崩溃!

如何解决此错误?

Logcat:

java.lang.NullPointerException
    at com.zackercracker.todthealien.GamePanel.surfaceCreated(GamePanel.java:63)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:569)
    at android.view.SurfaceView.access$000(SurfaceView.java:86)
    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:174)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:680)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1842)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:725)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)

我的主要活动课程:

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.ArrayList;
import java.util.Random;


public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
public static final int WIDTH = 960;
public static final int HEIGHT = 540;
public static final int MoveSpeed = -3;
private long enemyStartTime;
public MainThread thread;
private Background bg;
private Alien alien;
private ArrayList<Enemy> enemy;
private Random rand = new Random();

public GamePanel(Context context) {
    super(context);

    getHolder().addCallback(this);

    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int wight, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    boolean retry = true;
    while (retry) {
        try {
            thread.setRunning(false);
            thread.join();
            retry = false;
            thread = null;

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.backgrounda));

    enemy = new ArrayList<>();
    long EnemyElapsed = (System.nanoTime() - enemyStartTime) / 1000000;
    if (EnemyElapsed > (2000 - alien.getScore() / 4)) {
        switch ((int) (rand.nextDouble() * 5)) {
            case 0:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.dog), WIDTH + 10, 60, 250, 250, alien.getScore(), 2));
                enemyStartTime = System.nanoTime();
                break;
            case 1:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.hyd), WIDTH + 10, 60, 480, 270, alien.getScore(), 2));
                enemyStartTime = System.nanoTime();
                break;
            case 2:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.hobo), WIDTH + 10, 60, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
            case 3:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.hole), WIDTH + 10, 60, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
            case 4:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.baby), WIDTH + 10, 60, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
            case 5:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.lightpaul), WIDTH + 10, 45, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
        }
    }

    for (int i = 0; i < enemy.size(); i++) {
        enemy.get(i).update();
        if (collision(enemy.get(i), alien)) ;
        {
            enemy.remove(i);
            break;
        }
    }


    thread = new MainThread(getHolder(), this);
    thread.setRunning(true);
    thread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return super.onTouchEvent(event);
}

public void update() {
    bg.update();
    alien.update();
    for (Enemy m : enemy) {
        m.update();
    }
}

public boolean collision(GameObject a, GameObject b) {
    return Rect.intersects(a.getRectangel(), b.getRectangel());
}

public void draw(@NonNull Canvas canvas) {
    final float scaleFactoryX;
    scaleFactoryX = getWidth() / (WIDTH * 1.f);
    final float scaleFactoryY;
    scaleFactoryY = getHeight() / (HEIGHT * 1.f);
    final int savedState = canvas.save();
    canvas.scale(scaleFactoryX, scaleFactoryY);
    bg.draw(canvas);
    alien.draw(canvas);
    for (Enemy m : enemy) {
        m.draw(canvas);
    }
    canvas.restoreToCount(savedState);

}

我的游戏小组课程:

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.util.ArrayList;
import java.util.Random;


public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
public static final int WIDTH = 960;
public static final int HEIGHT = 540;
public static final int MoveSpeed = -3;
private long enemyStartTime;
public MainThread thread;
private Background bg;
private Alien alien;
private ArrayList<Enemy> enemy;
private Random rand = new Random();

public GamePanel(Context context) {
    super(context);

    getHolder().addCallback(this);

    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int wight, int height) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    boolean retry = true;
    while (retry) {
        try {
            thread.setRunning(false);
            thread.join();
            retry = false;
            thread = null;

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.backgrounda));

    enemy = new ArrayList<>();
    long EnemyElapsed = (System.nanoTime() - enemyStartTime) / 1000000;
    if (EnemyElapsed > (2000 - alien.getScore() / 4)) {
        switch ((int) (rand.nextDouble() * 5)) {
            case 0:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.dog), WIDTH + 10, 60, 250, 250, alien.getScore(), 2));
                enemyStartTime = System.nanoTime();
                break;
            case 1:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.hyd), WIDTH + 10, 60, 480, 270, alien.getScore(), 2));
                enemyStartTime = System.nanoTime();
                break;
            case 2:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.hobo), WIDTH + 10, 60, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
            case 3:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.hole), WIDTH + 10, 60, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
            case 4:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.baby), WIDTH + 10, 60, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
            case 5:
                enemy.add(new Enemy(BitmapFactory.decodeResource(getResources(), R.drawable.lightpaul), WIDTH + 10, 45, 580, 370, alien.getScore(), 1));
                enemyStartTime = System.nanoTime();
                break;
        }
    }

    for (int i = 0; i < enemy.size(); i++) {
        enemy.get(i).update();
        if (collision(enemy.get(i), alien)) ;
        {
            enemy.remove(i);
            break;
        }
    }


    thread = new MainThread(getHolder(), this);
    thread.setRunning(true);
    thread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return super.onTouchEvent(event);
}

public void update() {
    bg.update();
    alien.update();
    for (Enemy m : enemy) {
        m.update();
    }
}

public boolean collision(GameObject a, GameObject b) {
    return Rect.intersects(a.getRectangel(), b.getRectangel());
}

public void draw(@NonNull Canvas canvas) {
    final float scaleFactoryX;
    scaleFactoryX = getWidth() / (WIDTH * 1.f);
    final float scaleFactoryY;
    scaleFactoryY = getHeight() / (HEIGHT * 1.f);
    final int savedState = canvas.save();
    canvas.scale(scaleFactoryX, scaleFactoryY);
    bg.draw(canvas);
    alien.draw(canvas);
    for (Enemy m : enemy) {
        m.draw(canvas);
    }
    canvas.restoreToCount(savedState);

}

1 个答案:

答案 0 :(得分:1)

检查天气BitmapFactory.decodeResource returns null 找到你的63就是。

问题是: error opening trace file: No such file or directory (2)com.zackercracker.todthealien.GamePanel.surfaceCreated(GamePanel.java:63 这意味着你有空指针而不是正常值。