Android - 创建饼图时出现NullPointerException

时间:2013-11-15 06:42:53

标签: android charts pie-chart

我正在尝试通过从我的sqlite数据库中获取数据来创建饼图。我希望饼图显示朋友姓名和朋友消费。饼图应根据朋友消费进行划分。我在这做错了什么?请指导我。感谢。

这是我的饼图代码:

public class PieChart {
public Intent getIntent(Context context,String eventId){
    EventController controller = new EventController(context);
  // this is my data of performance; data is collected in array.
    HashMap<String, String> queryValues = new HashMap<String, String>();
    queryValues.put("eventId", eventId);
    ArrayList <HashMap<String,String>> getData = controller.getAllFriends(queryValues);
     CategorySeries series = new CategorySeries("pie"); // adding series to charts.
     int colorcode = -13959039;
     ArrayList<String> coloring = new ArrayList<String>();
    for (int a=0; a<getData.size();a++){
        String friendName = null;
        double friendSpend = 0;
        HashMap<String, String> ValexpenseId = getData.get(a);
        for (Entry<String, String> entry : ValexpenseId.entrySet()) {
            String key = entry.getKey();
            if (key.equals("friendName")){
                String value = entry.getValue();
                friendName = value;
            }
            else if (key.equals("friendSpending")){
                String value = entry.getValue();
                double friendSpending = Double.parseDouble(value);
                friendSpend = friendSpending;
            }
            series.add(friendName,friendSpend);             
            coloring.add(String.valueOf(colorcode));
            colorcode = colorcode + 1000000;
        }
    }            
 // set style for series
        DefaultRenderer renderer = new DefaultRenderer();
        for(String color : coloring){
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            int colour = Integer.parseInt(color);
            r.setColor(colour);
            r.setDisplayBoundingPoints(true);
            r.setDisplayChartValuesDistance(5);
            r.setDisplayChartValues(true);
            r.setChartValuesTextSize(15);
            renderer.addSeriesRenderer(r);
        }
        renderer.isInScroll();
        renderer.setZoomButtonsVisible(true);   //set zoom button in Graph
        renderer.setApplyBackgroundColor(true);
        renderer.setBackgroundColor(Color.BLACK); //set background color
        renderer.setChartTitle("Efforts");
        renderer.setChartTitleTextSize((float) 30);
        renderer.setShowLabels(true);  
        renderer.setLabelsTextSize(20);
        renderer.setLegendTextSize(25);
        renderer.setDisplayValues(true);
        return ChartFactory.getPieChartIntent(context, series, renderer, "PieChart");
    }

Log Cat:

11-15 15:03:33.985: E/AndroidRuntime(24170): FATAL EXCEPTION: main 11-15 15:03:33.985: E/AndroidRuntime(24170): java.lang.NullPointerException 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.chart.AbstractChart.getFitText(AbstractChart.java:405) 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.chart.AbstractChart.drawLabel(AbstractChart.java:483) 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.chart.PieChart.draw(PieChart.java:134) 11-15 15:03:33.985: E/AndroidRuntime(24170): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:168) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.draw(View.java:13877) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.getDisplayList(View.java:12811) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.getDisplayList(View.java:12859) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.draw(View.java:13593) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.ViewGroup.drawChild(ViewGroup.java:2954) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2815) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.draw(View.java:13880) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.widget.FrameLayout.draw(FrameLayout.java:467) 11-15 15:03:33.985: E/AndroidRuntime(24170): at android.view.View.getDisplayList(View.java:12811)

1 个答案:

答案 0 :(得分:0)

代码是如此多。我建议你划分方法。

1)准备数据

private CategorySeries getDatSet(WorkExp exp) {
        CategorySeries mSeries = new CategorySeries("");
        int[] weightArr = exp.getWeight();
        String[] taskArr = exp.getTask();
        for (int taskCount = 0; taskCount < taskArr.length; taskCount++) {
            int weight = weightArr[taskCount];
            String task = taskArr[taskCount];
            mSeries.add(task, weight);
        }
        return mSeries;
    }

2)生成渲染器

private DefaultRenderer getRenderer(WorkExp exp) {
        String[] colorArr = exp.getColor();
        DefaultRenderer defaultRenderer = new DefaultRenderer();
        for (int taskCount = 0; taskCount < colorArr.length; taskCount++) {
            String color = colorArr[taskCount];
            SimpleSeriesRenderer simpleRenderer = new SimpleSeriesRenderer();
            simpleRenderer.setColor(Color.parseColor(color));
            defaultRenderer.addSeriesRenderer(simpleRenderer);
        }
        defaultRenderer.setShowLabels(true);
        defaultRenderer.setShowLegend(true);
        return defaultRenderer;
    }

3)然后生成图表

private GraphicalView generateChart(CategorySeries dataset,
            DefaultRenderer renderer) {
        return ChartFactory.getPieChartView(context, dataset, renderer);
    }

4)最后致电

WorkExp exp = expList.get(position);
            chartContainer.addView(generateChart(getDatSet(exp),
                    getRenderer(exp)));

我使用相同的代码在两天前完成了饼图。

如果您不理解任何一行,请告诉我。我会帮助你的。