首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MPAndroidChart BarChart坠机

MPAndroidChart BarChart坠机
EN

Stack Overflow用户
提问于 2015-07-10 15:43:02
回答 1查看 1.5K关注 0票数 0

我正在尝试实现来自MPAndroidChartMPAndroidChart。当我从条形图中调用setData()方法时,会得到以下错误:

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

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

我真的抄袭了样本代码。怎么一回事?我正在使用列表视图中的图表

代码语言:javascript
复制
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类如下所示:

代码语言:javascript
复制
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‘

EN

回答 1

Stack Overflow用户

发布于 2015-07-11 17:58:43

你坠机的原因是:

代码语言:javascript
复制
@Override
public String getFormattedValue(float value)  {
    return null;
}

您不能从ValueFormatter返回 null。相反,如果不希望在轴上显示任何内容,请调用setDrawLabels(false)或从getFormattedValue(...)方法返回""

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31344966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档