Android WeakReference到位图null

Android WeakReference到位图null,android,null,surfaceview,weak-references,Android,Null,Surfaceview,Weak References,我正在使用自定义的PaintSurface类: public class PaintSurface extends SurfaceView implements SurfaceHolder.Callback { private Bitmap mapa; private WeakReference<Bitmap> timon; int rotacion=0; public static int parado=0; pub

我正在使用自定义的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;
    }
}
公共类PaintSurface扩展了SurfaceView
实现SurfaceHolder.Callback{
私有位图映射;
私人威克参考丁满;
int rotacion=0;
公共静态intparado=0;
公共漆面(上下文){
这个(上下文,空);
}
公共漆面(上下文、属性集属性){
超级(上下文,attrs);
getHolder().addCallback(此);
}
受保护的void OnelMedRaw(帆布画布、PointF nuevo、PointF viejo){
双角度1=calcRotationAngleInDegrees(viejo,新墨西哥州);
日志d(“myTag”,“OnElmeuDraw llamado con angulo:”+angle1);
float mCoordX=(float)viejo.x;
float mCoordY=(float)viejo.y;
Log.d(“myTag2”,“Voy a en漆面绘制位图时间:”+timon.get().toString()+“宽度:”+timon.get().getWidth()+“高度:”+timon.get().getHeight());
int-mitadpantalax=MainActivity.screenWidth/2;
int-mitadpantalay=MainActivity.screenHeight/2;
Rect corte=新的Rect((int)mCoordX-mitadpantalax,(int)mCoordY-mitadpantalax,((int)mCoordX+mitadpantalax),((int)mCoordY+mitadpantalax));
Rect pantalla=新的Rect(0,0,MainActivity.screenWidth,MainActivity.screenHeight);
drawBitmap(mapa、corte、pantalla、null);
位图平面=getResizedBitmap(BitmapFactory.decodeResource(getContext().getResources(),R.drawable.plane),200200);
平面=旋转位图(平面,(浮动)角度1);
canvas.drawBitmap(平面,mitadPantallaX平面.getWidth()/2,mitadPantallaY平面.getHeight()/2,null);
rotacion=rotacion+3;
如果(parado==0){
RotateBitmap(timon.get(),rotacion);
}
drawBitmap(timon.get(),((float)(midadpantallax-timon.get().getWidth()/2))-250,((float)(midadpantallay-timon.get().getHeight()/2)),null);
}
@凌驾
已创建的公共空隙表面(表面层表面层){
timon=newweakreference(BitmapFactory.decodeResource(getContext().getResources(),R.drawable.timo));
Log.d(“myTag”,“油漆表面创建的acabo de asignar timon”+timon.toString()+“con-timog.get()”+timon.get().toString());
}
公共静态位图旋转位图(位图源、浮动角度)
{
矩阵=新矩阵();
矩阵。旋转后(角度);
返回Bitmap.createBitmap(source,0,0,source.getWidth(),source.getHeight(),matrix,true);
}
公共静态双calcRotationAngleInDegrees(点F中心点,点F目标点){
双θ=数学atan2(targetPt.y-centerPt.y,targetPt.x-centerPt.x);
θ+=Math.PI/2.0;
双角度=数学toDegrees(θ);
如果(角度<0){
角度+=360;
}
返回角;
}
}
调用
surfaceCreated
时,我将一个新的
WeakReference
分配给一个局部变量,下一个
log
timon.toString()
timon.get().toString()提供一个
非空的
字符串作为结果:

acabo de asignar创建的漆面 timonjava.lang.ref。WeakReference@8f80da0contimog.get() android.graphics。Bitmap@f84bd59

但是当调用
onElMeuDraw
时,
日志会生成错误:

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
值的,这是什么时候发生的…

请添加日志以便更好地理解。已编辑!我希望它能让您更好地理解..查看代码最可能的情况是,在创建曲面的异步回调之前,您正在调用onElMeuDraw方法。