【问题标题】:Handler not fetching data to View处理程序未获取数据以查看
【发布时间】:2013-03-16 14:36:26
【问题描述】:

我正在尝试使用处理程序将消息传递给View 类;但不知道我哪里出错了。请纠正我。从 Handler 获取值后,我从来没有使用 canvas 进行绘制。

提前致谢!

运行此程序后,我的日志猫在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();
}


}

【问题讨论】:

    标签: view android-canvas android-handler


    【解决方案1】:

    处理程序总是只连接到一个线程,除非我们没有线程,否则它不能工作。即在主线程和后台线程之间发送或接收数据。

    View类的正确程序应该如下:

    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) {
    
    
    }
    
    
    
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多