如何在片段中解决getActivity NullPointerException?

时间:2018-03-14 11:26:04

标签: java android mpandroidchart

我知道这可能是愚蠢的问,但我找不到和相关的搜索结果,以满足我的要求,当我击中条形图它完美的颜色,但在第二次条形图颜色给出空指针异常是有任何解决方案,我使用条形图我的代码是:

LocationManager

我的日志是:

  ArrayList<Integer> colors = new ArrayList<Integer>();
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_blue_bright));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_purple));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_green_dark));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_red_dark));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_orange_light));

            set1.setColors(colors);
            mChart.setExtraBottomOffset(0);
            BarData data = new BarData(dataSets);
            data.setBarWidth(0.9f);
            data.setValueFormatter(new MyValueFormatter());
            XAxis xAxis =  mChart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setLabelRotationAngle(30);
            xAxis.setSpaceMax(5f);
            xAxis.setGranularity(1f);
            xAxis.setDrawGridLines(false);
            xAxis.setGranularityEnabled(true);
            Legend l= mChart.getLegend();
            LegendEntry l1=new LegendEntry("Activities", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_blue_bright));
            LegendEntry l2=new LegendEntry("ArmedActivated", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_purple));
            LegendEntry l3=new LegendEntry("Theft Averted", Legend.LegendForm.DEFAULT,10f,1f,null,ContextCompat.getColor(getContext(),android.R.color.holo_green_dark));
            LegendEntry l4=new LegendEntry("Arrest", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_red_dark));
            LegendEntry l5=new LegendEntry("Missed", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_orange_light));
            mChart.setFitBars(true);
            l.setEnabled(true);
            l.setWordWrapEnabled(true);
            l.setCustom(new LegendEntry[]{l1,l2,l3,l4,l5});
            mChart.setData(data);
            mChart.invalidate();

编辑:在这里我知道主要问题是getActivity只有任何一个人能在我的案例中为我提供解决方案。

0 个答案:

没有答案