MPAndroidChart Stacked BarChart Custom Markerview Nullpointer异常

时间:2015-12-22 07:31:18

标签: android mpandroidchart

我遇到了自定义标记示例的问题,我完全复制并将其设置在Stacked Barchart上,但是当我点击它时,我得到一个例外。

代码与example完全相同。

我得到的例外情况如下:

12-22 15:28:54.052 12901-12901/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.myapp.myapp, PID: 12901
                                               java.lang.NullPointerException: Attempt to read from field 'float com.github.mikephil.charting.highlight.Range.to' on a null object reference
                                                   at com.github.mikephil.charting.charts.BarLineChartBase.getMarkerPosition(BarLineChartBase.java:571)
                                                   at com.github.mikephil.charting.charts.Chart.drawMarkers(Chart.java:598)
                                                   at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:252)
                                                   at android.view.View.draw(View.java:15114)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14048)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.draw(View.java:15117)
                                                   at android.support.v4.view.ViewPager.draw(ViewPager.java:2262)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14048)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1229)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1077)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                   at android.view.ViewGroup.drawChild(ViewGroup.java:3404)
                                                   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
                                                   at android.view.View.updateDisplayListIfDirty(View.java:14043)
                                                   at android.view.View.getDisplayList(View.java:14071)
                                                   at android.view.View.draw(View.java:14838)
                                                at android.view.

有人有任何想法吗?

修改

所以这是onValueSelected(..)

    @Override
    public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
        barChart.highlightValue(e.getXIndex(), dataSetIndex);
    }

1 个答案:

答案 0 :(得分:1)

无论如何,检查Entry是否等于零是好的,因为它在Stacked BarChart示例中显示。

这是示例中的代码:

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

    BarEntry entry = (BarEntry) e;

    if (entry.getVals() != null)
        Log.i("VAL SELECTED", "Value: " + entry.getVals()[h.getStackIndex()]);
    else
        Log.i("VAL SELECTED", "Value: " + entry.getVal());
}