0

I am trying to pass message using handler to View class; but don't know where I am going wrong. Please correct me. I never used canvas to draw after getting values from Handler.

Thanks in advance!

My log-cat is showing NullPointerException on onDraw method after running this program.And hence line can not be drawn.

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();
}


}
Neha Somani
  • 102
  • 2
  • 11

1 Answers1

0

Handler is always attached to only one thread and unless and until we don't have thread, it can't work. i.e send or receive data between Main thread and background thread.

The correct program for View class should be as follows:

public class List extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new TestView(this));


}


}

 class TestView extends View {


Bitmap bitmap=Bitmap.createBitmap(1000, 1000, Config.ARGB_8888);
Canvas canvas=new Canvas(bitmap);
Paint p=new Paint();


Handler handler=new Handler(){

    @Override
    public void handleMessage(Message msg) {

        p.setColor(Color.BLUE);
        Bundle bundle=msg.getData();
        String name=bundle.getString("Name");
        Toast.makeText(getContext(), name, Toast.LENGTH_LONG).show();



    }

};

public TestView(Context context) {
    super(context);
    Paint p=new Paint();
    p.setColor(Color.BLUE);
    new Thread(){
        public void run(){

            Message msg=new Message();
            Bundle bundle=new Bundle();
            Bundle bundle1=new Bundle();
            Looper.prepare();
            bundle.putString("Name", "Nishant");
            bundle1.putString("Name", "Neha");
            msg.setData(bundle);
            handler.sendMessage(msg);
            Message m1=new Message();
            m1.setData(bundle1);
            handler.sendMessage(m1);




        }
    }.start();


}


@Override
protected void onDraw(Canvas canvas) {


}





}
Neha Somani
  • 102
  • 2
  • 11