尝试使用 Android Drawing 绘制图形时出现 NullPointerException

发布于 2024-11-06 04:33:01 字数 2349 浏览 1 评论 0原文

我想绘制一个跟踪用户体重进展的图表,
我正在编写一个可以在 onCreat 事件中调用的方法,
这是我的代码:

 public void drawGraph(){
    Display display = getWindowManager().getDefaultDisplay();   
private int width = display.getWidth();  
private int height = display.getHeight();                                               
    Paint paint = new Paint();
    Canvas canvas =new Canvas();
    paint.setColor(Color.GRAY);
    canvas.drawLine(0, 0, 0, height, paint); // the x-axes represent the date        
    canvas.drawLine(0, height, width, height, paint); // the y-axes represent the weight  

    paint.setColor(Color.RED);
    canvas.drawLine(0, max, width, max, paint); //draw the maximum healthy weight
    paint.setColor(Color.YELLOW);
    canvas.drawLine(0, min, width, min, paint); // draw the minimum healthy weight
    paint.setColor(Color.GREEN);
    canvas.drawLine(0, gW, width, gW, paint); // draw the goal weight
            int xDis = width/weekNumbers;
            int y;
    Path path = new Path();     
    for (int i = 0; i <= Weightvalues; i++){
        Cursor c = db.rawQuery("SELECT CURRENTWEIGHT FROM WEIGHT WHERE DATECREATED > " + startDate, null);
        int weight;
        if (c!=null){
            if (c.moveToFirst()){
                do{
                    weight = c.getInt(0);
                    // I want now to find out for each entry the point represent it on the graph
                    y = Math.round(weight * y / range); //range is the difference between the maximum weight and the minimum weight
                    if (i==1)
                        path.moveTo(0, y);
                    else
                        path.lineTo(0 + i*xDis, height-y);

                }while(c.moveToNext());
            }
        }
    }
    paint.setColor(Color.BLUE);
    canvas.drawPath(path, paint);

}  

这是 onCreat 事件

protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.weight_chart);
      helper = new DataBaseHelper(this);
      drawGraph();
}

,当我运行程序时,我遇到了这个错误

    ERROR/AndroidRuntime(738): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{*********.WeightChart}: java.lang.NullPointerException

任何人都可以帮我检查一下并告诉我哪里出了问题

最好的问候

I want to sketch a graph that track the user weight progress,
I am writing a method that I can call in the onCreat event,
this is my code:

 public void drawGraph(){
    Display display = getWindowManager().getDefaultDisplay();   
private int width = display.getWidth();  
private int height = display.getHeight();                                               
    Paint paint = new Paint();
    Canvas canvas =new Canvas();
    paint.setColor(Color.GRAY);
    canvas.drawLine(0, 0, 0, height, paint); // the x-axes represent the date        
    canvas.drawLine(0, height, width, height, paint); // the y-axes represent the weight  

    paint.setColor(Color.RED);
    canvas.drawLine(0, max, width, max, paint); //draw the maximum healthy weight
    paint.setColor(Color.YELLOW);
    canvas.drawLine(0, min, width, min, paint); // draw the minimum healthy weight
    paint.setColor(Color.GREEN);
    canvas.drawLine(0, gW, width, gW, paint); // draw the goal weight
            int xDis = width/weekNumbers;
            int y;
    Path path = new Path();     
    for (int i = 0; i <= Weightvalues; i++){
        Cursor c = db.rawQuery("SELECT CURRENTWEIGHT FROM WEIGHT WHERE DATECREATED > " + startDate, null);
        int weight;
        if (c!=null){
            if (c.moveToFirst()){
                do{
                    weight = c.getInt(0);
                    // I want now to find out for each entry the point represent it on the graph
                    y = Math.round(weight * y / range); //range is the difference between the maximum weight and the minimum weight
                    if (i==1)
                        path.moveTo(0, y);
                    else
                        path.lineTo(0 + i*xDis, height-y);

                }while(c.moveToNext());
            }
        }
    }
    paint.setColor(Color.BLUE);
    canvas.drawPath(path, paint);

}  

this is the onCreat event

protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.weight_chart);
      helper = new DataBaseHelper(this);
      drawGraph();
}

when i run the program i faced this error

    ERROR/AndroidRuntime(738): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{*********.WeightChart}: java.lang.NullPointerException

Can any body review it for me and tell me where i went wrong

Best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2024-11-13 04:33:01

您似乎使用 Canvas 时没有为其分配位图来进行绘图。要在屏幕上显示图像,一种方法是在布局 xml 中定义 ImageView;

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/graph_image" />

对您的drawGraph方法进行以下更改;

...
Bitmap bitmap = BitmapCreateBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas = new Canvas(bitmap);
...
Do your drawing stuff..
...
ImageView iv = (ImageView)findViewById(R.id.graph_image);
iv.setImageBitmap(bitmap);
}

You seem to use Canvas without assigning it a Bitmap to do drawing onto. To get your image on screen, one way is to define ImageView in your layout xml;

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/graph_image" />

Accompanied with following changes to your drawGraph method;

...
Bitmap bitmap = BitmapCreateBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas = new Canvas(bitmap);
...
Do your drawing stuff..
...
ImageView iv = (ImageView)findViewById(R.id.graph_image);
iv.setImageBitmap(bitmap);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文