如何解决addView导致NullPointerException
| 美好的一天! 我正在实现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();
}
}
有什么想法,这个错误的原因是什么?
解决方法
问题出在您的xml文件中。
android:name=\"@+id/price_chart\"
应该
android:id=\"@+id/price_chart\"
, 对您的XML文件进行任何更改,然后保存并再次运行。当我今晚早些时候遇到类似问题时,为我工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。