美好的一天! 我正在實現aChartEngine在Android上繪製圖表,並面臨一個麻煩:當嘗試添加創建圖表視圖到佈局與addView方法時,我收到NullPointerException。addView cause NullPointerException
chart_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/charts_relative_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:name="@+id/price_chart"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
// other elements
</RelativeLayout>
Charts.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
//other code
}
protected void onResume() {
super.onResume();
if (chart_price == null) {
LinearLayout layout = (LinearLayout) findViewById(R.id.price_chart);
chart_price = ChartFactory.getLineChartView(this, buildDataset(getTitles(), dateArray, priceArray), getRenderrer(paramList.get(0).length, paramList.get(1).length, maxPrice));
layout.addView(chart_price); // THIS LINE CAUSES NULLPOINTEREXCEPTION
}
else {
chart_price.repaint();
}
}
是什麼想法,有什麼可這個錯誤的原因是什麼?
你已經證實,它是是導致NPE還是它的chart_price佈局? – Noel 2011-06-12 00:20:26
我使用下面的代碼,它顯示chart_price不爲null。我的猜測是,錯誤會導致addView方法或chart_layout.xml的設置。 ::::::::::::::::: System.out.println(「在ADDVIEW之前」); \t \t System.out.println(chart_price == null); \t \t layout.addView(chart_price); – dimzul 2011-06-12 05:38:50