Error in code of basic game using multiple sprites and surfaceView [on hold]
- by Khagendra Nath Mahato
I am a beginner to android and i was trying to make a basic game with the help of an online video tutorial. I am having problem with the multi-sprites and how to use with surfaceview.The application fails launching. Here is the code of the game.please help me.
package com.example.killthemall;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class Game extends Activity {
    KhogenView View1;
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        while(true){
        try {
            OurThread.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }}
    }
    Thread OurThread;
    int herorows = 4;
    int herocolumns = 3;
    int xpos, ypos;
    int xspeed;
    int yspeed;
    int herowidth;
    int widthnumber = 0;
    int heroheight;
    Rect src;
    Rect dst;
    int round;
    Bitmap bmp1;
    // private Bitmap bmp1;//change name
    public List<Sprite> sprites = new ArrayList<Sprite>() {
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        View1 = new KhogenView(this);
        setContentView(View1);
        sprites.add(createSprite(R.drawable.image));
        sprites.add(createSprite(R.drawable.bad1));
        sprites.add(createSprite(R.drawable.bad2));
        sprites.add(createSprite(R.drawable.bad3));
        sprites.add(createSprite(R.drawable.bad4));
        sprites.add(createSprite(R.drawable.bad5));
        sprites.add(createSprite(R.drawable.bad6));
        sprites.add(createSprite(R.drawable.good1));
        sprites.add(createSprite(R.drawable.good2));
        sprites.add(createSprite(R.drawable.good3));
        sprites.add(createSprite(R.drawable.good4));
        sprites.add(createSprite(R.drawable.good5));
        sprites.add(createSprite(R.drawable.good6));
    }
    private Sprite createSprite(int image) {
        // TODO Auto-generated method stub
        bmp1 = BitmapFactory.decodeResource(getResources(), image);
        return new Sprite(this, bmp1);
    }
    public class KhogenView extends SurfaceView implements Runnable {
        SurfaceHolder OurHolder;
        Canvas canvas = null;
        Random rnd = new Random();
        {
            xpos = rnd.nextInt(canvas.getWidth() - herowidth)+herowidth;
            ypos = rnd.nextInt(canvas.getHeight() - heroheight)+heroheight;
            xspeed = rnd.nextInt(10 - 5) + 5;
            yspeed = rnd.nextInt(10 - 5) + 5;
        }
        public KhogenView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            OurHolder = getHolder();
            OurThread = new Thread(this);
            OurThread.start();
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            herowidth = bmp1.getWidth() / 3;
            heroheight = bmp1.getHeight() / 4;
            boolean isRunning = true;
            while (isRunning) {
                if (!OurHolder.getSurface().isValid())
                    continue;
                canvas = OurHolder.lockCanvas();
                canvas.drawRGB(02, 02, 50);
                for (Sprite sprite : sprites) {
                    if (widthnumber == 3)
                        widthnumber = 0;
                    update();
                    getdirection();
                    src = new Rect(widthnumber * herowidth, round * heroheight,
                            (widthnumber + 1) * herowidth, (round + 1)* heroheight);
                    dst = new Rect(xpos, ypos, xpos + herowidth, ypos+ heroheight);
                    canvas.drawBitmap(bmp1, src, dst, null);
                }
                widthnumber++;
                OurHolder.unlockCanvasAndPost(canvas);
            }
        }
        public void update() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            if (xpos + xspeed <= 0)
                xspeed = 40;
            if (xpos >= canvas.getWidth() - herowidth)
                xspeed = -50;
            if (ypos + yspeed <= 0)
                yspeed = 45;
            if (ypos >= canvas.getHeight() - heroheight)
                yspeed = -55;
            xpos = xpos + xspeed;
            ypos = ypos + yspeed;
        }
        public void getdirection() {
            double angleinteger = (Math.atan2(yspeed, xspeed)) / (Math.PI / 2);
            round = (int) (Math.round(angleinteger) + 2) % herorows;
            // Toast.makeText(this, String.valueOf(round),
            // Toast.LENGTH_LONG).show();
        }
    }
    public class Sprite {
        Game game;
        private Bitmap bmp;
        public Sprite(Game game, Bitmap bmp) {
            // TODO Auto-generated constructor stub
            this.game = game;
            this.bmp = bmp;
        }
    }
}
Here is the LogCat if it helps....
08-22 23:18:06.980: D/AndroidRuntime(28151): Shutting down VM
08-22 23:18:06.980: W/dalvikvm(28151): threadid=1: thread exiting with uncaught exception (group=0xb3f6f4f0)
08-22 23:18:06.980: D/AndroidRuntime(28151): procName from cmdline: com.example.killthemall
08-22 23:18:06.980: E/AndroidRuntime(28151): in writeCrashedAppName, pkgName :com.example.killthemall
08-22 23:18:06.980: D/AndroidRuntime(28151): file written successfully with content: com.example.killthemall StringBuffer : ;com.example.killthemall
08-22 23:18:06.990: I/Process(28151): Sending signal. PID: 28151 SIG: 9
08-22 23:18:06.990: E/AndroidRuntime(28151): FATAL EXCEPTION: main
08-22 23:18:06.990: E/AndroidRuntime(28151): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.killthemall/com.example.killthemall.Game}: java.lang.NullPointerException
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.os.Looper.loop(Looper.java:130)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.app.ActivityThread.main(ActivityThread.java:3683)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at java.lang.reflect.Method.invoke(Method.java:507)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at dalvik.system.NativeStart.main(Native Method)
08-22 23:18:06.990: E/AndroidRuntime(28151): Caused by: java.lang.NullPointerException
08-22 23:18:06.990: E/AndroidRuntime(28151):    at com.example.killthemall.Game$KhogenView.<init>(Game.java:96)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at com.example.killthemall.Game.onCreate(Game.java:58)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-22 23:18:06.990: E/AndroidRuntime(28151):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-22 23:18:06.990: E/AndroidRuntime(28151):    ... 11 more
08-22 23:18:18.050: D/AndroidRuntime(28191): Shutting down VM
08-22 23:18:18.050: W/dalvikvm(28191): threadid=1: thread exiting with uncaught exception (group=0xb3f6f4f0)
08-22 23:18:18.050: I/Process(28191): Sending signal. PID: 28191 SIG: 9
08-22 23:18:18.050: D/AndroidRuntime(28191): procName from cmdline: com.example.killthemall
08-22 23:18:18.050: E/AndroidRuntime(28191): in writeCrashedAppName, pkgName :com.example.killthemall
08-22 23:18:18.050: D/AndroidRuntime(28191): file written successfully with content: com.example.killthemall StringBuffer : ;com.example.killthemall
08-22 23:18:18.050: E/AndroidRuntime(28191): FATAL EXCEPTION: main
08-22 23:18:18.050: E/AndroidRuntime(28191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.killthemall/com.example.killthemall.Game}: java.lang.NullPointerException
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.os.Looper.loop(Looper.java:130)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.app.ActivityThread.main(ActivityThread.java:3683)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at java.lang.reflect.Method.invoke(Method.java:507)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at dalvik.system.NativeStart.main(Native Method)
08-22 23:18:18.050: E/AndroidRuntime(28191): Caused by: java.lang.NullPointerException
08-22 23:18:18.050: E/AndroidRuntime(28191):    at com.example.killthemall.Game$KhogenView.<init>(Game.java:96)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at com.example.killthemall.Game.onCreate(Game.java:58)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-22 23:18:18.050: E/AndroidRuntime(28191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-22 23:18:18.050: E/AndroidRuntime(28191):    ... 11 more