美好的一天! 我正在实现aChartEngine在Android上绘制图表并遇到麻烦:我尝试使用addView方法将创建的图表视图添加到布局时收到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();
}
}
有任何想法,这可能是造成这种错误的原因吗?
答案 0 :(得分:3)
问题出在您的xml文件中。
android:name="@+id/price_chart"
应该是
android:id="@+id/price_chart"
答案 1 :(得分:0)
对XML文件进行任何更改,然后再次保存并运行它。当我今晚早些时候遇到类似的问题时为我工作。