2011-05-16 35 views
-1

這裏是我的主要活動課這裏是我的android系統中以下錯誤條形圖代碼我得到

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文件沒有任何內容視圖,所以請檢查它。

回答

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; 

看看你是否創建一個實例,並通過nullvalues會發生什麼? 本地變量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子類實例已經創建與其他構造函數它已被定義並且不會初始化字段。只需向其他構造函數體添加一條日誌語句來檢查是否屬於這種情況。

+0

我仍然得到錯誤,並沒有得到結果下面的錯誤05-16 17:37:59.042:信息/ ActivityManager(71):進程android.graph.graphview(pid 2496)已經去世 – Herry 2011-05-16 12:12:16

+0

@Herry - 哪一行是第159行?我不想數線(你在這條線上仍然有NPE,對吧?) – 2011-05-16 12:16:18

+0

@Andreas_d是的,我仍然在線159上得到NPE的錯誤。 – Herry 2011-05-16 12:22:10

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()方法中。

相關問題