Toast 和 Looper,一个属于 android.widget,一个属于 android.os,两个貌似联系不怎么紧密的类,却通过下面这个异常联系到了一起:
E/AndroidRuntime( 1819): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() E/AndroidRuntime( 1819): at android.os.Handler.<init>(Handler.java:121) E/AndroidRuntime( 1819): at android.widget.Toast.<init>(Toast.java:397) E/AndroidRuntime( 1819): at android.widget.Toast.makeText(Toast.java:230) E/AndroidRuntime( 1819): at android.widget.Toast.makeText(Toast.java:256)
由以上的错误信息可以看出:程序要创建 handler,但是发现 Looper.prepare 还没有被调用。通过 Android SDK 中的 Reference 可以看到,Looper、Handler 的调用是非常有讲究的,如下面示例代码:
class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { // process incoming messages here } }; Looper.loop(); } }
言归正题,继续寻找 Toast、Looper 和 Handler 三者之间的联系,也许谜底就能解开了。欲揭谜底,从源码入手是一条捷径。
Toast.java 的第230行的代码是创建一个新的 Toast 实例,而实例化的过程中,就需要执行第397行,也就是声明并创建 Handler 的实例。那么来看 Handler.java 的第121行到底做了什么,如下所示:
mLooper = Looper.myLooper(); if (mLooper == null) { throw new RuntimeException( "Can't create handler inside thread that has not called Looper.prepare()"); }
到此,距离真相的解开近了一大步,既然抛出了 RuntimeException,那么 mLooper 肯定是 null,但是为什么 Looper.myLooper() 会返回 null?继续进入到 Looper.java 中寻根究底。
/** * Return the Looper object associated with the current thread. Returns * null if the calling thread is not associated with a Looper. */ public static final Looper myLooper() { return (Looper)sThreadLocal.get(); }
以上就是 myLooper() 方法的真实面貌,通过注释可以看出问题的真正原因在于当前线程并没有绑定 Looper,返回为 null 是正确但非正常的结果。
问题的根本原因已经解开,但是另外一个疑团也就产生了:为何当前线程没有 Looper 呢?经过对代码进行 review,原因找到了:当事者在 non-UI 线程进行 Toast.makeText
package com.touch;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class TouchActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
MyView view = new MyView(this);
setContentView(view);
view.requestFocus();
}
}
class MyView extends View{
private Paint mPaint, pointPaint;
private Bitmap mBitmap;
Context mContext;
int n;
public MyView(Context context) {
super(context);
mContext = context;
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setARGB(0, 0xff, 0, 0);
pointPaint = new Paint();
pointPaint.setARGB(150, 0, 255, 255);
pointPaint.setTextSize(30);
DisplayMetrics dm = new DisplayMetrics();
dm=getResources().getDisplayMetrics();
mBitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels, Bitmap.Config.RGB_565);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(mBitmap != null)
canvas.drawBitmap(mBitmap, 0, 0, null);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int N = event.getHistorySize();
float x = 0;
float y = 0;
x = event.getX();
y = event.getY();
Canvas canvas = new Canvas();
canvas.setBitmap(mBitmap);
if(event.getAction() == MotionEvent.ACTION_DOWN){
mPaint.setARGB(0xff, 0, 0, 0);
canvas.drawPaint(mPaint);
n=0;
invalidate();
}else{
++n;
mPaint.setARGB(100, 0xff, 0, 0);
canvas.drawCircle(x, y, 4, mPaint);
invalidate();
}
if(event.getAction() == MotionEvent.ACTION_UP){
canvas.drawText("点个数="+n, mBitmap.getWidth()/2-30, 30, pointPaint);
invalidate();
}
return true;
}
}
Entity:
An
Entitiy is an object that can be drawn, like Sprites, Rectangles, Text
or Lines. An Entity has a position/rotation/scale/color/etc...
一个entitiy是一个可以被画的物体,像Sprites,矩形,文本或线。一个实体有一个位置/旋转/重力/彩色/等等
1.IEntity接口
public interface IEntity extends IDrawable, IUpdateHandler { public int getZIndex(); public void setZIndex(final int pZIndex); }
2.IDrawable接口
public interface IDrawable { public void onDraw(final GL10 pGL, final Camera pCamera); }
3.IUpdateHandler接口
public interface IUpdateHandler { public void onUpdate(final float pSecondsElapsed); public void reset(); }