我正在使用自定义PaintSurface类:
public class PaintSurface extends SurfaceView
implements SurfaceHolder.Callback {
private Bitmap mapa;
private WeakReference<Bitmap> timon;
int rotacion=0;
public static int parado=0;
public PaintSurface(Context context) {
this(context, null);
}
public PaintSurface(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
}
protected void onElMeuDraw(Canvas canvas,PointF nuevo,PointF viejo) {
double angle1 = calcRotationAngleInDegrees(viejo,nuevo);
Log.d("myTag", "OnElmeuDraw llamado con angulo: "+ angle1 );
float mCoordX = (float)viejo.x;
float mCoordY = (float)viejo.y;
Log.d("myTag2", "Voy a en paintsurface drawbitmap con timom:"+ timon.get().toString() + " width: "+timon.get().getWidth()+ "height: " + timon.get().getHeight() );
int mitadPantallaX = MainActivity.screenWidth/2;
int mitadPantallaY = MainActivity.screenHeight/2;
Rect corte =new Rect((int)mCoordX-mitadPantallaX,(int)mCoordY-mitadPantallaY,((int)mCoordX+mitadPantallaX),((int)mCoordY+mitadPantallaY));
Rect pantalla = new Rect(0,0,MainActivity.screenWidth,MainActivity.screenHeight);
canvas.drawBitmap(mapa,corte,pantalla,null);
Bitmap plane = getResizedBitmap(BitmapFactory.decodeResource(getContext().getResources(),R.drawable.plane),200,200);
plane = RotateBitmap(plane,(float)angle1);
canvas.drawBitmap(plane,mitadPantallaX-plane.getWidth()/2,mitadPantallaY-plane.getHeight()/2,null);
rotacion=rotacion +3;
if(parado ==0 ){
RotateBitmap(timon.get(), rotacion);
}
canvas.drawBitmap(timon.get(), ((float) (mitadPantallaX - timon.get().getWidth()/2)) - 250, ((float) (mitadPantallaY -timon.get().getHeight()/2)) , null);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
timon = new WeakReference<Bitmap>(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.timo));
Log.d("myTag", "paintSurface surface created acabo de asignar timon" + timon.toString() + "con timog.get() " + timon.get().toString());
}
public static Bitmap RotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
public static double calcRotationAngleInDegrees(PointF centerPt, PointF targetPt) {
double theta = Math.atan2(targetPt.y - centerPt.y, targetPt.x - centerPt.x);
theta += Math.PI / 2.0;
double angle = Math.toDegrees(theta);
if (angle < 0) {
angle += 360;
}
return angle;
}
}
调用surfaceCreated
后,我将新WeakReference
分配给本地变量,下一个log
为non-null
和timon.toString()
字符串提供timon.get().toString()
字符串{1}}:
paintSurface表面创建了acabo de asignar timonjava.lang.ref.WeakReference@8f80da0con timog.get() android.graphics.Bitmap@f84bd59
但是当onElMeuDraw
被调用时,logs
会产生错误:
W / System.err:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String java.lang.Object.toString()' 04-13 03:34:21.727 16639-16967 / com.example.ivan.prac2 W / System.err:at com.example.ivan.prac2.PaintSurface.onElMeuDraw(PaintSurface.java:65) 04-13 03:34:21.728 16639-16967 / com.example.ivan.prac2 W / System.err:at com.example.ivan.prac2.FilPaint.run(FilPaint.java:75)
我不明白局部变量如何获得null
值以及何时发生这种情况......