问题描述
- 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 }
}
解决方案
thread exiting with uncaught exception
线程退出了因为没有捕获异常
com.himi.MySurfaceView.draw(MySurfaceView.java:66)
com.himi.MySurfaceView.run(MySurfaceView.java:85)
看看上面两行代码放入try里面捕获下