Hello guys I got a Null Pointer Exception if I want to open a graph in a fragment.
This is my exception code:
Graph.onCreate(Bundle) line: 39
Graph(Fragment).performCreate(Bundle) line: 1755
FragmentManagerImpl.moveToState(Fragment, int, int, int, boolean) line: 868
FragmentManagerImpl.moveToState(int, int, int, boolean) line: 1106
BackStackRecord.run() line: 690
FragmentManagerImpl.execPendingActions() line: 1571
FragmentManagerImpl$1.run() line: 447
Handler.handleCallback(Message) line: 733
Handler.dispatchMessage(Message) line: 95
This is my Fragment code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.graph_fragment);
plot = (XYPlot) getView().findViewById(R.id.mySimpleXYPlot3);
// Create a couple arrays of y-values to plot:
Number[] series1Numbers = {1, 8, 5, 2, 7, 4,3,5,12,50,10,20};
Number[] series2Numbers = {4, 6, 3, 8, 2, 100};
// Turn the above arrays into XYSeries':
XYSeries series1 = new SimpleXYSeries(
Arrays.asList(series1Numbers), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Series1"); // Set the display title of the series
// same as above
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
// Create a formatter to use for drawing a series using LineAndPointRenderer
// and configure it from xml:
LineAndPointFormatter series1Format = new LineAndPointFormatter();
series1Format.setPointLabelFormatter(new PointLabelFormatter());
series1Format.configure(getActivity().getApplicationContext(),
R.xml.line_point_formatter_with_plf1);
// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);
// same as above:
LineAndPointFormatter series2Format = new LineAndPointFormatter();
series2Format.setPointLabelFormatter(new PointLabelFormatter());
series2Format.configure(getActivity().getApplicationContext(),
R.xml.line_point_formatter_with_plf2);
plot.addSeries(series2, series2Format);
// reduce the number of range labels
plot.setTicksPerRangeLabel(1);
plot.setDomainStepValue(series1Numbers.length);
plot.getGraphWidget().setDomainLabelOrientation(-45);
How can I open the graph without the null pointer exception? Can I load the data in my activity and add them to the Fragment? Would that change something? Thanks for your help:)
You need to override oncreateView and inflate your xml file there. Try this :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.graph_fragment, container,
false);
//your plot would look like this:
plot = (XYPlot) view.findViewById(R.id.mySimpleXYPlot3);
return view;
}
Its generally a good practice to override onStart method and use the objects on this method.
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
plot.addSeries(series1, series1Format);
}