2011-06-11 5 views
0

좋은 하루! Android에서 차트를 그리기 위해 ChartEngine을 구현 중이며 문제가 발생했습니다. 생성 된 차트 뷰를 addView 메소드로 레이아웃에 추가하려고하면 NullPointerException이 발생합니다.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

해야 하는가? – Noel

+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

답변

3

문제는 XML 파일에 있습니다.

android:name="@+id/price_chart" 

당신이이 NPE가 발생하거나 그것이 chart_price의 여부 레이아웃의 확인 했

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

내 실수를 지적 해 주셔서 감사합니다! 이제 제대로 작동합니다! – dimzul

0

XML 파일을 변경 한 다음 저장하고 다시 실행하십시오. 오늘 밤에 문제가 생겼을 때 저를 위해 일했습니다.

+0

이 파일을 여러 번 변경했지만 긍정적 인 효과가 없습니다. RelativeLayout을 LinearLayout으로 변경하려고 시도했지만 ... 너무 도움이되지 않았습니다. 정확히 어떤 변화가 도움이 되었습니까? – dimzul

+0

Noel이 문제에 대한 답변을 게시 했으므로 문제가 아니므로 Noel을 무시하십시오. – Fraggle

관련 문제