Fatal exception (Thread)

Ich wollte etwas Wasser kräuselt sich in meine android-Anwendung so habe ich diese javascript-code http://jsfiddle.net/esteewhy/5Ht3b/6/ umgewandelt in C-code wie hier gezeigt Live Wallpaper Mit Wasser Wellen-Effekt (von esteewhy).

Ich habe einen main-Aktivität, die fordert, die Wasser-Wellen-Aktivität nach dem Klick auf eine Schaltfläche. Die Wasser-Wellen funktionieren gut mit meinem veränderlich bitmap, das problem ist, wenn ich klickte auf den "zurück" - button des android-Gerät, das system stürzt ab, wirft diesen Fehler:

FATAL EXCEPTION: Thread-2556
java.lang.NullPointerException
at com.wheelly.whater.WaterView.onDraw (line 149)
at com.wheelly.whater.WaterView$GameThread.run (line 255)

Ich denke, es gibt einige problem bei der Verarbeitung der Thread oder die Aktivitäten selbst.

Hier ist der code, der das Wasser Kräuselt:

public class WaterView extends SurfaceView implements SurfaceHolder.Callback {
    GameThread thread;

    public static Bitmap icon;

    //Measure frames per second.
    long now;
    int framesCount=0;
    int framesCountAvg=0;
    long framesTimer=0;
    Paint fpsPaint=new Paint();

    //Frame speed
    long timeNow;
    long timePrev = 0;
    long timePrevFrame = 0;
    long timeDelta;

    private int width = 480;
    private int height = 800;

    private short riprad = 6;
    boolean flip;
    private short[] ripplemap, last_map;
    Bitmap ripple;

    private static final String TAG = null;

    private Rippler rippler;

    public WaterView(Context context) {
        super(context);
        initialize();
    }

    void initialize() {
        //CB 
        icon = BitmapFactory.decodeResource(getResources(), R.drawable.sand100);
        icon = convertToMutable(icon);
        //--
        rippler = new NativeRippler();
        reinitgGlobals();
        fpsPaint.setTextSize(30);

        //Set thread
        getHolder().addCallback(this);

        setFocusable(true);
    }

    void reinitgGlobals() {
        int size = width * (height + 2) * 2;
        ripplemap = new short[size];
        last_map = new short[size];
        Bitmap texture = createBackground(width, height); //this creates a MUTABLE bitmap
        ripple = texture;
        _td = new int[width * height];
        texture.getPixels(_td, 0, width, 0, 0, width, height);
        _rd = new int[width * height];
    }

    void randomizer() {
        final Random rnd = new Random();
        final Handler disHAndler = new Handler();
        final Runnable disturbWater = new Runnable() {
            @Override
            public void run() {
                disturb(rnd.nextInt(width), rnd.nextInt(height));
                disHAndler.postDelayed(this, 7000);
            }
        };
        disHAndler.post(disturbWater);
    }

    private static Bitmap createBackground(int width, int height) {
        Canvas cb = new Canvas (icon);
        cb.save();
        cb.restore();
        return icon;
    }


    private Bitmap convertToMutable(Bitmap bitmap) {
        try {
            File file = new File("/mnt/sdcard/sample/temp.txt");
            file.getParentFile().mkdirs();
            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

            int width_mutable = bitmap.getWidth();
            int height_mutable = bitmap.getHeight();

            FileChannel channel = randomAccessFile.getChannel();
            MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, width_mutable*height_mutable*4);
            bitmap.copyPixelsToBuffer(map);
            bitmap.recycle();

            bitmap = Bitmap.createBitmap(width_mutable, height_mutable, Config.ARGB_8888);
            map.position(0);
            bitmap.copyPixelsFromBuffer(map);

            channel.close();
            randomAccessFile.close();

        } catch (FileNotFoundException e) {
            Log.i(TAG, "::onActivityResult:" + ""+Log.getStackTraceString(e));
        } catch (IOException e) {
            Log.i(TAG, "::onActivityResult:" + ""+Log.getStackTraceString(e));
        }
        return bitmap;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        reinitgGlobals();
    }

    @Override
    protected void onDraw(android.graphics.Canvas canvas) {
        super.onDraw(canvas);
        newframe();
        canvas.drawBitmap(ripple, 0, 0, null);

        //Measure frame rate (unit: frames per second).
        now=System.currentTimeMillis();
        canvas.drawText(framesCountAvg+" fps", 40, 70, fpsPaint);
        framesCount++;
        if(now-framesTimer>1000) {
                framesTimer=now;
                framesCountAvg=framesCount;
                framesCount=0;
        }
    }

    /**
     * Disturb water at specified point
     */
    private void disturb(int dx, int dy) {
        rippler.disturb(dx, dy, width, height, riprad, ripplemap, flip);
    }

    int[] _td;
    int[] _rd;

    /**
     * Generates new ripples
     */
    private void newframe() {
        System.arraycopy(_td, 0, _rd, 0, width * height);
        flip = !flip;
        rippler.transformRipples(height, width, ripplemap, last_map, _td, _rd, flip);
        ripple.setPixels(_rd, 0, width, 0, 0, width, height);
    }


    @Override
    public synchronized boolean onTouchEvent(MotionEvent event) {
            disturb((int)event.getX(), (int)event.getY());
            return true;

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        thread = new GameThread(getHolder(), this);
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {

            }
        }
    }

    class GameThread extends Thread {
        private SurfaceHolder surfaceHolder;
        private WaterView gameView;
        private boolean run = false;

        public GameThread(SurfaceHolder surfaceHolder, WaterView gameView) {
            this.surfaceHolder = surfaceHolder;
            this.gameView = gameView;
        }

        public void setRunning(boolean run) {
            this.run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (run) {
                c = null;

                //limit frame rate to max 60fps
                timeNow = System.currentTimeMillis();
                timeDelta = timeNow - timePrevFrame;
                if ( timeDelta < 16) {
                    try {
                        Thread.sleep(16 - timeDelta);
                    }
                    catch(InterruptedException e) {
                    }
                }
                timePrevFrame = System.currentTimeMillis();

                try {
                    c = surfaceHolder.lockCanvas(null);
                    synchronized (surfaceHolder) {
                       //call methods to draw and process next fame
                        gameView.onDraw(c);
                    }
                } finally {
                    if (c != null) {
                        surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
}
  • Die Linie 149?
  • Hi, diese Zeile hier: Leinwand.drawBitmap(ripple, 0, 0, null);
Schreibe einen Kommentar