这是我在android中的条形图的代码以下错误我得到

时间:2011-05-16 11:33:03

标签: java android canvas draw bar-chart

这是我的主要活动类

    package android.graph.graphview;

    import android.app.Activity;
    import android.graphics.Canvas;
    import android.os.Bundle;

    public class GraphView extends Activity {
/** Called when the activity is first created. */
GraphViewDemo mGraphViewDemo;
Canvas mCanvas = new Canvas();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mGraphViewDemo = (GraphViewDemo) findViewById(R.id.graphview);
    float[] values = new float[] { 1.0f, 6.0f, 7.0f, 8.0f, 9.0f, 3.0f,
            2.0f, 10.0f, 6.0f, 7.0f, 8.0f, 9.0f, 1.0f };
    String[] verlabels = new String[] { "great", "ok", "bad" };
    String[] horlabels = new String[] { "today", "tomorrow", "next week",
            "next month", "Next Year" };
    mGraphViewDemo = new GraphViewDemo(this, values, "GraphViewDemo",
            horlabels, verlabels, GraphViewDemo.BAR);
    mGraphViewDemo.draw(mCanvas);

}

}

这是我的类,我在GraphViewDemo.java中为绘制条形图定义视图

    package android.graph.graphview;

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Paint.Align;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;

    public class GraphViewDemo extends View {

public static boolean BAR = true;
public static boolean LINE = false;

private Paint paint;
private float[] values;
private String[] horlabels;
private String[] verlabels;
private String title;
private boolean type;

public GraphViewDemo(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public GraphViewDemo(Context context, float[] values, String title,
        String[] horlabels, String[] verlabels, boolean type) {
    super(context);
    if (values == null)
        values = new float[0];
    else
        this.values = values;
    if (title == null)
        title = "";
    else
        this.title = title;
    if (horlabels == null)
        this.horlabels = new String[0];
    else
        this.horlabels = horlabels;
    if (verlabels == null)
        this.verlabels = new String[0];
    else
        this.verlabels = verlabels;
    this.type = type;
    paint = new Paint();
}

@Override
protected void onDraw(Canvas canvas) {
    float border = 20;
    float horstart = border * 2;
    float height = getHeight();
    float width = getWidth() - 1;
    float max = getMax();    << also here i get NPE error in my Logcat >>
    float min = getMin();
    float diff = max - min;
    float graphheight = height - (2 * border);
    float graphwidth = width - (2 * border);


    paint.setTextAlign(Align.LEFT);
    int vers = verlabels.length - 1;
    for (int i = 0; i < verlabels.length; i++) {
        // paint.setColor(Color.RED);
        float y = ((graphheight / vers) * i) + border;
        // canvas.drawLine(horstart, y, width, y, paint);
        paint.setColor(Color.WHITE);
        canvas.drawText(verlabels[i], 0, y, paint);
    }
    int hors = horlabels.length - 1;
    for (int i = 0; i < horlabels.length; i++) {
        // paint.setColor(Color.BLUE);
        float x = ((graphwidth / hors) * i) + horstart;
        // canvas.drawLine(x, height - 50, x, border, paint);
        paint.setTextAlign(Align.CENTER);
        if (i == horlabels.length - 1)
            paint.setTextAlign(Align.RIGHT);
        if (i == 0)
            paint.setTextAlign(Align.LEFT);
        paint.setColor(Color.WHITE);
        canvas.drawText(horlabels[i], x, height - 4, paint);
    }

    paint.setTextAlign(Align.CENTER);
    canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);

    if (max != min) {
        paint.setColor(Color.LTGRAY);
        if (type == BAR) {
            int minus = 10;
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                canvas.drawRect((i * colwidth) + horstart,
                        ((border - h) + graphheight) + 100,
                        (((i * colwidth) + horstart) + (colwidth - 1))
                                - minus, ((height - (border - 1) - 50)),
                        paint);
                // (border - h)+ graphheight

            }

        }
    }
}

private float getMax() {
    float largest = Integer.MIN_VALUE;
    for (int i = 0; i < values.length; i++)   <<in this line i get NPE
        // if (values[i] > largest)
        // largest = values[i];
        largest = 10;
    Log.v("log_tag", "All Data max of getmax " + largest);
    return largest;
}

private float getMin() {
    float smallest = Integer.MAX_VALUE;
    for (int i = 0; i < values.length; i++)
        // if (values[i] < smallest)
        smallest = -5;
    Log.v("log_tag", "All Data min get min  " + smallest);
    return smallest;
}

}

现在这是我的main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:text="@string/hello" />
<android.graph.graphview.GraphViewDemo
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/graphview" />
</LinearLayout>

这是我的错误所以请解决这个问题。

    05-16 16:43:17.392: ERROR/AndroidRuntime(822): FATAL EXCEPTION: main
    05-16 16:43:17.392: ERROR/AndroidRuntime(822): java.lang.NullPointerException
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at            android.graph.graphview.GraphViewDemo.getMax(GraphViewDemo.java:159)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.graph.graphview.GraphViewDemo.onDraw(GraphViewDemo.java:57)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.View.draw(View.java:6740)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640)      
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367) 
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.View.draw(View.java:6743)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.drawChild(ViewGroup.java:1640 )
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.View.draw(View.java:6743)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.widget.FrameLayout.draw(FrameLayout.java:352)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewRoot.draw(ViewRoot.java:1407)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.os.Handler.dispatchMessage(Handler.java:99)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.os.Looper.loop(Looper.java:123)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at java.lang.reflect.Method.invokeNative(Native Method)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at java.lang.reflect.Method.invoke(Method.java:521)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    05-16 16:43:17.392: ERROR/AndroidRuntime(822):     at dalvik.system.NativeStart.main(Native Method)

我希望xml文件中的此解决方案不是任何内容视图,因此请检查它。

2 个答案:

答案 0 :(得分:2)

mGraphViewDemo = new GraphViewDemo(this, values, "GraphViewDemo",
        horlabels, verlabels, GraphViewDemo.BAR);
mGraphViewDemo.draw(mCanvas);

我认为,你的意图是覆盖已经膨胀的视图对象。这不会这样做,所以你仍然有视图,它没有正确初始化,因此得到NullPointerException。从xml中删除视图,只需将创建的(在onCreate())视图中添加到布局中。

setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);// don't forget to mention id in xml
float[] values = new float[] { 1.0f, 6.0f, 7.0f, 8.0f, 9.0f, 3.0f,
        2.0f, 10.0f, 6.0f, 7.0f, 8.0f, 9.0f, 1.0f };
String[] verlabels = new String[] { "great", "ok", "bad" };
String[] horlabels = new String[] { "today", "tomorrow", "next week",
        "next month", "Next Year" };
mGraphViewDemo = new GraphViewDemo(this, values, "GraphViewDemo",
        horlabels, verlabels, GraphViewDemo.BAR);
layout.add(mGraphViewDemo);

此外,您不应该实例化Canvas对象,也不应该手动尝试绘制视图。必须将所有自定义绘图放在视图的onDraw()方法中。

答案 1 :(得分:0)

getMax中NPE的唯一原因是values等于null

这是在此代码的构造函数中启动的:

public GraphViewDemo(Context context, float[] values, String title,
    String[] horlabels, String[] verlabels, boolean type) {
  super(context);

  if (values == null)
    values = new float[0];
  else
    this.values = values;

看看如果您创建实例并为null传递values会发生什么?使用零大小数组初始化本地变量values,而类属性 values仍为null

这将解决问题:

  if (values == null)
    this.values = new float[0];  // <-- change in this line
  else
    this.values = values;

(顺便说一句 - 一个非常常见的错误......)


View州的Javadoc:

View(Context context, AttributeSet attrs)
    Constructor that is called when inflating a view from XML.

很有可能,您的实际View子类实例已使用定义的其他构造函数创建,并且不会初始化字段。只需将log语句添加到其他构造函数体,以检查是否是这种情况。