MPAndroidChart BarChart Crash

时间:2015-07-10 15:43:03

标签: android mpandroidchart

我正在尝试从BarChart实施MPAndroidChart。当我从条形图调用setData()方法时,出现以下错误:

java.lang.NullPointerException:尝试调用虚方法' int java.lang.String.length()'在空对象引用上

崩溃发生在以下地方:com.github.mikephil.charting.components.YAxis.getLongestLabel(YAxis.java:333)

我真的复制了示例代码。到底是怎么回事?我在listview中使用图表

static class DailyGoalViewHolder  {

    public BarChart mChart;    
}

priate DailyGoalViewHolder mDailyGoalViewHolder;

@Override
private void getView(int position, View convertView, ViewGroup Parent)  {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.layout_dashboard_daily_goal, parent, false);

    mDailyGoalViewHolder = new DailyGoalViewHolder();
    mDailyGoalViewHolder.mChart = (BarChart) convertView.findViewById(R.id.chart);

    mDailyGoalViewHolder.mChart.setDrawBarShadow(false);
    mDailyGoalViewHolder.mChart.setDrawValueAboveBar(true);
    mDailyGoalViewHolder.mChart.setDescription("");
    mDailyGoalViewHolder.mChart.setMaxVisibleValueCount(60);
    mDailyGoalViewHolder.mChart.setPinchZoom(false);
    mDailyGoalViewHolder.mChart.setDrawGridBackground(false);

    XAxis xAxis = mDailyGoalViewHolder.mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setSpaceBetweenLabels(2);

    ValueFormatter custom = new ChartValueFormatter();

    YAxis leftAxis = mDailyGoalViewHolder.mChart.getAxisLeft();
    leftAxis.setLabelCount(8);
    leftAxis.setValueFormatter(custom);
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setSpaceTop(15f);

    Legend l = mDailyGoalViewHolder.mChart.getLegend();
    l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
    l.setForm(Legend.LegendForm.SQUARE);
    l.setFormSize(9f);
    l.setTextSize(11f);
    l.setXEntrySpace(4f);

    setDataforChart(12, 50);
}

private void setData(int count, float range) {

    ArrayList<String> xVals = new ArrayList<String>();
    for (int i = 0; i < count; i++) {
        xVals.add("TEST");
    }

    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

    for (int i = 0; i < count; i++) {
        float mult = (range + 1);
        float val = (float) (Math.random() * mult);
        yVals1.add(new BarEntry(val, i));
    }

    BarDataSet set1 = new BarDataSet(yVals1, "DataSet");
    set1.setBarSpacePercent(35f);

    ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
    dataSets.add(set1);

    BarData data = new BarData(xVals, dataSets);
    data.setValueTextSize(10f);
    data.setValueTypeface(mTf);

    mDailyGoalViewHolder.mChart.setData(data);
}

在单独的文件中,ChartValueFomatter类如下:

import com.github.mikephil.charting.utils.ValueFormatter;

import java.text.DecimalFormat;

public class ChartValueFormatter implements ValueFormatter  {

    private DecimalFormat mFormat;

    public ChartValueFormatter()  {

        mFormat = new DecimalFormat("###,###,###,##0.0");
    }

    @Override
    public String getFormattedValue(float value)  {

        return null;
    }
}

Gradle - &gt;     编译&#39; com.github.PhilJay:MPAndroidChart:v2.1.0&#39;

1 个答案:

答案 0 :(得分:1)

崩溃的原因是:

@Override
public String getFormattedValue(float value)  {
    return null;
}

您无法从null返回 ValueFormatter。相反,如果您不想在轴上显示任何内容,请致电setDrawLabels(false)或从""方法返回getFormattedValue(...)