片段中的空指针异常AndroidPlot

时间:2014-06-24 14:43:49

标签: android eclipse android-fragments graph androidplot

大家好,如果我想在片段中打开图形,我会得到一个空指针异常。

这是我的异常代码:

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   

这是我的片段代码:

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);  

如何在没有空指针异常的情况下打开图形? 我可以在活动中加载数据并将它们添加到Fragment中吗? 这会改变什么吗? 谢谢你的帮助:)

2 个答案:

答案 0 :(得分:2)

onCreateView之前调用onCreate,getViewonCreateView完成之前返回null,使您的应用崩溃

 plot = (XYPlot) getView().findViewById(R.id.mySimpleXYPlot3);

您可以覆盖onViewCreated并移动该方法中的所有初始化人员

答案 1 :(得分:2)

您需要覆盖oncreateView并在那里膨胀您的xml文件。试试这个:

@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;
}

一般来说,覆盖onStart方法并使用此方法上的对象通常是一种很好的做法。

@Override
public void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    plot.addSeries(series1, series1Format);
}