0
처리기를 사용하여 View
클래스에 메시지를 전달하려고합니다. 그러나 내가 어디로 잘못 가고 있는지 모릅니다. 제발 고쳐주세요. Handler에서 값을 가져온 후에 그릴 때 캔버스를 사용하지 않았습니다.처리기가 데이터를 가져 오지 못했습니다.
미리 감사드립니다.
내 로그 - 고양이는이 프로그램을 실행 한 후 onDraw
메서드에 NullPointerException
을 표시하고 있습니다. 따라서 라인을 그릴 수 없습니다.
public class List extends Activity{
Handler handler=new Handler();
Message msg=new Message();
Bundle bundle=new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TestView(this));
bundle.putFloat("x1", 10);
bundle.putFloat("y1", 10);
bundle.putFloat("x2", 100);
bundle.putFloat("y2", 100);
msg.setData(bundle);
handler.sendMessage(msg);
}
}
class TestView extends View {
Paint p;
float x1;
float y1;
float x2;
float y2;
public TestView(Context context) {
super(context);
Paint p=new Paint();
p.setColor(Color.BLUE);
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
Looper.prepare();
Bundle bundle=msg.getData();
x1=bundle.getFloat("x1");
y1=bundle.getFloat("y1");
x2=bundle.getFloat("x2");
y2=bundle.getFloat("y2");
Looper.loop();
}
};
}
boolean isDrawing=true;
@Override
protected void onDraw(Canvas canvas) {
canvas.drawLine(x1, y1, x2, y2, p);
invalidate();
}
}