美好的一天! 我正在实现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