尝试使用 Android Drawing 绘制图形时出现 NullPointerException
我想绘制一个跟踪用户体重进展的图表,
我正在编写一个可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎使用 Canvas 时没有为其分配位图来进行绘图。要在屏幕上显示图像,一种方法是在布局 xml 中定义 ImageView;
对您的drawGraph方法进行以下更改;
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;
Accompanied with following changes to your drawGraph method;