achartengine导致空指针

时间:2014-06-02 11:56:06

标签: android achartengine

关于包org.achartengine(可用版本1.2.0和1.1.0)我有问题。

在我的应用程序中,我调用新活动,传递参数,然后填充图形数据。

所有这些都属于同一类:

private void showChart(CustomerTransactionModel ctm, String title) {
    try {
        if (ctm==null || ctm.getTransactionGroup()==null || 
                ctm.getTransactionGroup().getTransactionTemplate()==null || 
                ctm.getTransactionGroup().getTransactionTemplate().getAxisXProperty()==null ||
                ctm.getTransactionGroup().getTransactionTemplate().getAxisYProperty()==null ||
                ctm.getTransactionGroup().getTransactions()==null) {
            return;
        }
        String xKey=ctm.getTransactionGroup().getTransactionTemplate().getAxisXProperty();
        String yKey=ctm.getTransactionGroup().getTransactionTemplate().getAxisYProperty();

        ArrayList<String[]> v = new ArrayList<String[]>();
        for (Transaction tran : ctm.getTransactionGroup().getTransactions()) {
            if (tran!=null && tran.getServiceData()!=null) {
                Map<Object, Object> serviceData = tran.getServiceData();

                if (serviceData.containsKey(xKey) && serviceData.containsKey(yKey)) {
                    String[] add=new String[2];
                    add[0]=serviceData.get(xKey).toString();
                    add[1]=serviceData.get(yKey).toString();

                    v.add(add);
                }
            }
        }
        if (!v.isEmpty()) {
            openChartDialog(v, title, xKey, yKey);
        }
    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(), e);
    }
}

private void openChartDialog(ArrayList<String[]> v, String title, String xLabel, String yLabel) {
    try {
        Object[] objects=new Object[4];
        objects[0]=xLabel;
        objects[1]=yLabel;
        objects[2]=title;
        objects[3]=v;
        OpeningChartTask task = new OpeningChartTask();
        task.doInBackground(objects);

    } catch (Exception e) {
        logger.error(e.getLocalizedMessage(), e);
    }
}

private class OpeningChartTask extends AsyncTask<Object, Object, Object> {
    @Override
    protected Object doInBackground(Object... params) {
        String xLabel=(String) params[0];
        String yLabel=(String) params[1];
        String title=(String) params[2];
        ArrayList<String[]> v=(ArrayList<String[]>) params[3];

        Intent intent = new Intent(getActivity(), ChartDialogActivity.class);
        intent.putExtra("xLabel", xLabel);
        intent.putExtra("yLabel", yLabel);
        intent.putExtra("title", title);
        intent.putExtra("values", v);
        startActivityForResult(intent, 0);
        return null;
    }
}

到目前为止一切顺利。在创建时,我读取了那些数据(它们是成功设置的),然后调用包含这个的方法openBarChart():

private void openBarChart() {
    // Create XY Series for X Series.
    XYSeries xSeries = new XYSeries("X series");
    // Adding data to the X Series.
    for (int i = 0; i < _data.size(); i++) {
        xSeries.add(Double.parseDouble(_data.get(i)[0]),
                Double.parseDouble(_data.get(i)[1]));
    }

    // Create XYSeriesRenderer to customize XSeries
    XYSeriesRenderer Xrenderer = new XYSeriesRenderer();
    Xrenderer.setColor(Color.GREEN);
    Xrenderer.setChartValuesTextSize(20);
    Xrenderer.setPointStyle(PointStyle.DIAMOND);
    Xrenderer.setDisplayChartValues(false);
    Xrenderer.setLineWidth(2);
    Xrenderer.setFillPoints(true);

    // Create XYMultipleSeriesRenderer to customize the whole chart
    final XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

    mRenderer.setChartTitle("X Vs Y Chart");
    mRenderer.setXTitle("X Values");
    mRenderer.setYTitle("Y Values");
    mRenderer.setZoomButtonsVisible(false);
    mRenderer.setXLabels(0);
    mRenderer.setPanEnabled(true);
    mRenderer.setAntialiasing(true);
    mRenderer.setBackgroundColor(Color.DKGRAY);
    mRenderer.setApplyBackgroundColor(true);
    mRenderer.setBarWidth(20);
    mRenderer.setScale((float) 0.3);

    mRenderer.setShowGrid(true);

    mRenderer.setClickEnabled(true);

    for (int i = 0; i < _data.size(); i++) {
        mRenderer.addXTextLabel(i, _data.get(i)[0]);
    }

    // Adding the XSeriesRenderer to the MultipleRenderer.
    mRenderer.addSeriesRenderer(Xrenderer);

    LinearLayout chart_container = (LinearLayout) findViewById(R.id.chartLayoutTest);

    // Create a Dataset to hold the XSeries.
    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

    // Add X series to the Dataset.
    dataset.addSeries(xSeries);

    // Creating an intent to plot line chart using dataset and
    // multipleRenderer
    final GraphicalView mChart;
    mChart = (GraphicalView) ChartFactory.getBarChartView(this, dataset,
            mRenderer, BarChart.Type.DEFAULT);

    // Add the graphical view mChart object into the Linear layout .
    chart_container.removeAllViews();
    chart_container.addView(mChart);
    if (mChart != null) {
        mChart.repaint();
    }
}

这种方法也存在,但是当它应该在屏幕上显示时会抛出错误并且应用程序死亡。

stacktrace如下:

E/ExceptionHandler( 8461): [uncaughtException] > ************ CAUSE OF ERROR ************
E/ExceptionHandler( 8461): 
E/ExceptionHandler( 8461): java.lang.NullPointerException
E/ExceptionHandler( 8461):  at java.util.TreeMap$BoundedMap.<init>(TreeMap.java:1115)
E/ExceptionHandler( 8461):  at java.util.TreeMap.headMap(TreeMap.java:713)
E/ExceptionHandler( 8461):  at  org.achartengine.model.XYSeries.getRange(XYSeries.java:310)
E/ExceptionHandler( 8461):  at org.achartengine.chart.XYChart.draw(XYChart.java:261)
E/ExceptionHandler( 8461):  at org.achartengine.GraphicalView.onDraw(GraphicalView.java:181)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13826)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13710)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.drawChild(ViewGroup.java:2988)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13829)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13710)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.drawChild(ViewGroup.java:2988)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13829)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13710)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.drawChild(ViewGroup.java:2988)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13708)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.drawChild(ViewGroup.java:2988)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13708)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.drawChild(ViewGroup.java:2988)
E/ExceptionHandler( 8461):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)
E/ExceptionHandler( 8461):  at android.view.View.draw(View.java:13829)
E/ExceptionHandler( 8461):  at android.widget.FrameLayout.draw(FrameLayout.java:467)
E/ExceptionHandler( 8461):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2458)
E/ExceptionHandler( 8461):  at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2382)
E/ExceptionHandler( 8461):  at android.view.ViewRootImpl.draw(ViewRootImpl.java:2275)
E/ExceptionHandler( 8461):  at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2143)
E/ExceptionHandler( 8461):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1954)
E/ExceptionHandler( 8461):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1108)
E/ExceptionHandler( 8461):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4491)
E/ExceptionHandler( 8461):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:755)
E/ExceptionHandler( 8461):  at android.view.Choreographer.doCallbacks(Choreographer.java:585)
E/ExceptionHandler( 8461):  at android.view.Choreographer.doFrame(Choreographer.java:555)
E/ExceptionHandler( 8461):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:741)
E/ExceptionHandler( 8461):  at android.os.Handler.handleCallback(Handler.java:615)
E/ExceptionHandler( 8461):  at android.os.Handler.dispatchMessage(Handler.java:92)
E/ExceptionHandler( 8461):  at android.os.Looper.loop(Looper.java:137)
E/ExceptionHandler( 8461):  at android.app.ActivityThread.main(ActivityThread.java:4949)
E/ExceptionHandler( 8461):  at java.lang.reflect.Method.invokeNative(Native Method)
E/ExceptionHandler( 8461):  at java.lang.reflect.Method.invoke(Method.java:511)
E/ExceptionHandler( 8461):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
E/ExceptionHandler( 8461):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
E/ExceptionHandler( 8461):  at dalvik.system.NativeStart.main(Native Method)

非常感谢任何帮助!

哦,这个,我完成了工作并检查了那些.java文件的内容,发现了几个“调整”,但我想这是我在这里做错了。如果我创建新项目我通常可以使用这个jar,唯一的区别是之前我导入了jar但现在我用maven pom文件调用这个jar(我有自己的存储库但是它应该可以正常工作)。

谢谢!

***解决方案***

在类org.achartengine.model.XYSeries中,我创建了简单的Comparator并将其传递给两个TreeMap构造函数。然后我在org.achartengine.util.IndexXYMap中更改了构造函数以接受coparator并传递给super(比较器)。

我不确切地知道为什么在一个项目中发生这种情况并且之前没有这样做,所以如果有人对此有解释,我会留下问题。

感谢@keshav的努力。

0 个答案:

没有答案