2011-06-11 97 views
0

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

是什么想法,有什么可这个错误的原因是什么?

+0

你已经证实,它是是导致NPE还是它的chart_price布局? – Noel 2011-06-12 00:20:26

+0

我使用下面的代码,它显示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

回答

3

问题出在你的xml文件中。

​​

应该

android:id="@+id/price_chart" 
+0

非常感谢您指出我的错误!现在它正常工作! – dimzul 2011-06-12 20:10:19

0

对XML文件进行任何更改,然后保存并再次运行。当我今天早些时候遇到类似的问题时为我工作。

+0

我已经多次更改此文件,但没有任何积极影响。即使试图将RelativeLayout更改为LinearLayout ...也没有帮助。有什么变化帮助你? – dimzul 2011-06-12 05:43:37

+0

诺埃尔已经发布了你的问题的答案,所以不要理我,因为这不是你的问题。 – Fraggle 2011-06-12 12:16:29