addView provoque NullPointerException
-
27-10-2019 - |
Question
Bonjour! Je aChartEngine la mise en œuvre de dessiner des cartes sur Android et ont dû faire face à une difficulté. Je reçois NullPointerException lorsque vous essayez d'ajouter du graphique créé pour la mise en page avec la méthode addView
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();
}
}
Y a des idées, ce qui peut être la raison de cette erreur?
La solution
Le problème est dans votre fichier xml.
android:name="@+id/price_chart"
doit être
android:id="@+id/price_chart"
Autres conseils
Faire un changement dans votre fichier XML, puis enregistrer et exécuter à nouveau. A travaillé pour moi quand j'avais un problème plus tôt ce soir qui était similaire.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow