MPAndroidChart BarChart Crash

Я пытаюсь реализовать BarChart из MPAndroidChart. Когда я вызываю метод setData() из гистограммы, я получаю следующую ошибку:

java.lang.NullPointerException: попытка вызвать виртуальный метод 'int java.lang.String.length()' для нулевой ссылки на объект

Сбой происходит в следующем месте: com.github.mikephil.charting.components.YAxis.getLongestLabel(YAxis.java:333)

Я буквально скопировал пример кода. Что происходит? Я использую график внутри списка

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 --> скомпилировать 'com.github.PhilJay:MPAndroidChart:v2.1.0'


person andyjslee    schedule 10.07.2015    source источник
comment
Не могли бы вы добавить в свой код и файлы gradle?   -  person SJoshi    schedule 10.07.2015
comment
@SJoshi, я только что сделал. Спасибо   -  person andyjslee    schedule 10.07.2015
comment
Не могли бы вы также добавить трассировку стека?   -  person SJoshi    schedule 10.07.2015


Ответы (1)


Причина вашего сбоя в следующем:

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

Вы не можете вернуться null из ValueFormatter. Вместо этого, если вы не хотите ничего отображать на своей оси, вызовите setDrawLabels(false) или верните "" из метода getFormattedValue(...).

person Philipp Jahoda    schedule 11.07.2015