2 swx0923 swx0923 于 2014.05.15 14:51 提问

android开发报错log信息如下,小弟新手,求问是什么错误,错在哪,如何解决

02-21 20:41:34.969: W/dalvikvm(20654): threadid=11: thread exiting with uncaught exception (group=0x40abf228)
02-21 20:41:34.969: E/AndroidRuntime(20654): FATAL EXCEPTION: Thread-6366
02-21 20:41:34.969: E/AndroidRuntime(20654): java.lang.IllegalArgumentException
02-21 20:41:34.969: E/AndroidRuntime(20654): at android.view.Surface.unlockCanvasAndPost(Native Method)
02-21 20:41:34.969: E/AndroidRuntime(20654): at android.view.SurfaceView$4.unlockCanvasAndPost(SurfaceView.java:819)
02-21 20:41:34.969: E/AndroidRuntime(20654): at com.himi.MySurfaceView.draw(MySurfaceView.java:66)
02-21 20:41:34.969: E/AndroidRuntime(20654): at com.himi.MySurfaceView.run(MySurfaceView.java:85)
02-21 20:41:34.969: E/AndroidRuntime(20654): at java.lang.Thread.run(Thread.java:864)
02-21 20:41:35.089: D/memalloc(20654): /dev/pmem: Mapped buffer base:0x54554000 size:28688384 offset:26599424 fd:77
02-21 20:41:35.409: D/memalloc(20654): /dev/pmem: Unmapping buffer base:0x52a1a000 size:26599424 offset:24510464
02-21 20:41:35.409: D/memalloc(20654): /dev/pmem: Unmapping buffer base:0x54554000 size:28688384 offset:26599424
02-21 20:41:38.222: D/Process(20654): killProcess, pid=20654
02-21 20:41:38.222: D/Process(20654): dalvik.system.VMStack.getThreadStackTrace(Native Method)
02-21 20:41:38.252: D/Process(20654): java.lang.Thread.getStackTrace(Thread.java:599)
02-21 20:41:38.252: D/Process(20654): android.os.Process.killProcess(Process.java:790)
02-21 20:41:38.252: D/Process(20654): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)

enter code here

package com.himi;

import java.util.Vector;
//Download by http://www.codefans.net
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

public class MySurfaceView extends SurfaceView implements Callback, Runnable {
private Thread th;
private SurfaceHolder sfh;
private Canvas canvas;
private Paint p;
public static Vector vec_bmp;
public static Vector vec_string;
private int col;

public MySurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    p = new Paint();
    p.setAntiAlias(true);
    sfh = this.getHolder();
    sfh.addCallback(this);
    th = new Thread(this);
    this.setKeepScreenOn(true);
    setFocusable(true);
    vec_string = new Vector<String>();
    vec_bmp = new Vector<Bitmap>();
}

public void surfaceCreated(SurfaceHolder holder) {
    col = this.getWidth() / 100;
    th.start();
}

public void draw() {
    try {
        canvas = sfh.lockCanvas();
        if (canvas != null) {
            canvas.drawColor(Color.BLACK);
            if (vec_bmp != null && vec_bmp.size() != 0) {
                for (int i = 0; i < vec_bmp.size(); i++) {
                    Bitmap bitmap = vec_bmp.elementAt(i);
                    p.setStyle(Style.STROKE);
                    canvas.drawRect((i % col) * 104 + 1, (i / col) * 100 + 1, (i % col) * 104 + 104 - 2, (i / col) * 100 + 100 - 2, p);
                    canvas.drawBitmap(bitmap, (i % col) * 100, (i / col) * 100, p);
                    p.setColor(Color.YELLOW);
                    canvas.drawText(vec_string.elementAt(i), (i % col) * 104 + 10, (i / col) * 100 + 97, p);
                    p.setColor(Color.WHITE);
                }
            }

        }
    } catch (Exception e) {
        // TODO: handle exception
    } finally {
        sfh.unlockCanvasAndPost(canvas);
    }
}

@Override
public boolean onKeyDown(int key, KeyEvent event) {

    return super.onKeyDown(key, event);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    return true;
}

public void run() {
    // TODO Auto-generated method stub
    while (true) {
        draw();
        try {
            Thread.sleep(100);
        } catch (Exception ex) {
        }
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub

}

public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub

}

}

2个回答

wangzhen19891010
wangzhen19891010   2014.05.15 15:54

thread exiting with uncaught exception
线程退出了因为没有捕获异常
com.himi.MySurfaceView.draw(MySurfaceView.java:66)
com.himi.MySurfaceView.run(MySurfaceView.java:85)

看看上面两行代码放入try里面捕获下

swx0923
swx0923 我把代码已经发上来了,有时间就帮我看看呗
接近 4 年之前 回复
Coco_loser
Coco_loser   2016.12.07 21:54

似乎楼上说的不对吧他已经是Exception( try {
Thread.sleep(100);
} catch (Exception ex) {
})
捕获了现在是Canvas里有错误
.unlockCanvasAndPost(Native Method)

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!