0

抱歉标题简洁。我有一个 Android 应用程序,我在其中绘制了一个在 CanvasThread 上运行的类,以远离主 GUI 线程。但是现在,当我想根据从查询到 DataHelper 类的数据绘制矩形时。当我想实例化 DataHelper 时,DataHelper dh = new DataHelper(this);我收到抱怨说我需要 DataHelper 中的另一个构造函数,它将 PanelChart 作为参数而不是 Context。为什么是这样?

这是我的 PanelChart 类:

public class PanelChart extends SurfaceView implements SurfaceHolder.Callback {
private CanvasThread canvasthread ;
private SurfaceView sf;
private DataHelper dh ;

public PanelChart(Context context, AttributeSet attrs) {
    super(context, attrs);


getHolder().addCallback(this);
canvasthread = new CanvasThread(getHolder(), this);
sf = (SurfaceView) findViewById(R.id.SurfaceView01);
setFocusable(true);



  //getData(dh);

}

获取数据(dh);是从我想调用调用 DataHelper 并检索绘制所需信息的方法的地方。

我应该在 DataHelper 中创建另一个构造函数吗?我应该在另一个类中进行查询并将其发送到我的 PanelChart 类吗?

谢谢!

4

1 回答 1

1

我认为您应该使用 PanelChart 构造函数中的上下文参数,即 Activity 的上下文,您可以在其中创建 PanelChart:

public class PanelChart extends SurfaceView implements SurfaceHolder.Callback {
private CanvasThread canvasthread ;
private SurfaceView sf;
private DataHelper dh ;

public PanelChart(Context context, AttributeSet attrs) {
    super(context, attrs);

DataHelper dh = new DataHelper(context);
getHolder().addCallback(this);
canvasthread = new CanvasThread(getHolder(), this);
sf = (SurfaceView) findViewById(R.id.SurfaceView01);
setFocusable(true);


}
于 2011-06-04T08:46:18.723 回答