android 多线程异常,android-致命异常(线程)

我想在我的android应用程序中产生一些水波纹,所以我使用了将javascript代码http://jsfiddle.net/esteewhy/5Ht3b/6/转换为C代码的方式,就像这里显示的Live Wallpaper Water Ripple Effect(由esteewhy).

我有一个主要活动,单击一个按钮后会调用水波纹活动.水波纹与可变的位图配合使用效果很好,问题是当我单击android设备的“后退”按钮时,系统崩溃并抛出以下错误:

FATAL EXCEPTION: Thread-2556

java.lang.NullPointerException

at com.wheelly.whater.WaterView.onDraw (line 149)

at com.wheelly.whater.WaterView$GameThread.run (line 255)

我想在处理线程或活动本身时会遇到一些问题.

这是水波纹的代码:

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

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值