qq_43544413
qq_43544413
采纳率0%
2019-07-17 16:08 阅读 497

关于报错闪退的问题?有没有大神来看一眼。。。。

这是我做柱状图的代码

package com.chenxi.bimquanziguankong.fragment;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.chenxi.bimquanziguankong.R;
import com.chenxi.bimquanziguankong.appConst.AppConst;
import com.chenxi.bimquanziguankong.application.MyApplication;
import com.chenxi.bimquanziguankong.common.StringAxisValueFormatter;
import com.chenxi.bimquanziguankong.entity.InvestmentControlClass;
import com.chenxi.bimquanziguankong.view.LineChart02View;
import com.chenxi.bimquanziguankong.view.MyMarkerView;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;


public class InvestmentAmountControlFragment extends BaseFragment {
    private View mView;
    RelativeLayout rl_document_list_is_null;
    private CombinedChart combineChart;
    List<InvestmentControlClass> investmentControlClasses = new ArrayList<>();
    private List<String> xAxisValues = new ArrayList<>();
    XAxis xAxis;
    YAxis axisLeft;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_new_investment_amount_control, container, false);
        initView(mView);
        getxAxisValuesData();
        getyAxisValuesData();
        MyMarkerView myMarkerView = new MyMarkerView(getActivity(), R.layout.custom_marker_view);
        myMarkerView.setChartView(combineChart);
        combineChart.setMarker(myMarkerView);
        return mView;
    }


    private void initView(View mView) {
        rl_document_list_is_null = mView.findViewById(R.id.rl_document_list_is_null);
        combineChart = mView.findViewById(R.id.combineChart);
        combineChart.setDrawBorders(true); // 显示边界
        combineChart.getDescription().setEnabled(false);  // 不显示备注信息
        combineChart.setPinchZoom(true); // 比例缩放
    }

    private void getxAxisValuesData() {
        String url = AppConst.innerIp + "/api/" + AppConst.getProjectID() + "/InvestmentControlClass";
        Request request = new Request.Builder()
                .url(url)
                .build();
        MyApplication.getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                ToastUtils.showShort("数据请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() == 200) {
                try {
                    String responseData = response.body().string();
                    investmentControlClasses = JSON.parseArray(responseData, InvestmentControlClass.class);
                   if (investmentControlClasses.size()!=0){
                       for (int i = 0; i < investmentControlClasses.size(); i++) {
                           xAxisValues.add(investmentControlClasses.get(i).getName());
                       }
                   }
                    xAxis = combineChart.getXAxis();
                    xAxis.setDrawGridLines(false);
                    /*解决左右两端柱形图只显示一半的情况 只有使用CombinedChart时会出现,如果单独使用BarChart不会有这个问题*/
                    xAxis.setAxisMinimum(-0.5f);
                    xAxis.setAxisMaximum(xAxisValues.size() - 0.5f);
                    xAxis.setGranularity(1f);
                    xAxis.setLabelCount(xAxisValues.size()); // 设置X轴标签数量
                    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置X轴标签位置,BOTTOM在底部显示,TOP在顶部显示
                    xAxis.setValueFormatter(new StringAxisValueFormatter(xAxisValues));
                    axisLeft = combineChart.getAxisLeft(); // 获取左边Y轴操作类
                    axisLeft.setAxisMinimum(0); // 设置最小值
                    axisLeft.setGranularity(10); // 设置Label间隔
                    axisLeft.setLabelCount(10);
                }catch (IOException e){
                    e.printStackTrace();
                }
                } else {
                    ToastUtils.showShort("数据请求出错!500");
                }
            }
        });
    }

    private void getyAxisValuesData() {
        String url = AppConst.innerIp + "/api/" + AppConst.getProjectID() + "/Report/InvestmentControl";
        //请求数据
        Request fileRrequest = new Request.Builder()
                .url(url)
                .build();
        Log.e("TAG", "柱状图地址 "+fileRrequest );
        MyApplication.getOkHttpClient().newCall(fileRrequest).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                ToastUtils.showShort("数据请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() == 200) {
                    String responseData = response.body().string();
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            JSONArray jsonArray = null;
                            try {
                                jsonArray = new JSONArray(responseData);
                                //获取柱状图数组
                                List<BarEntry> barEntries = new ArrayList<>();
                                List<Entry> lineEntry = new ArrayList<>();
                                if (xAxisValues.size()!=0){
                                    for (int i = 0; i < xAxisValues.size(); i++) {
                                        List<String> list = new ArrayList<>();
                                        for (int j = 0; j < jsonArray.length(); j++) {
                                            JSONObject jo = new JSONObject(jsonArray.get(j).toString());
                                            if (i > 0) {
                                                list.add(jo.getString(xAxisValues.get(i)));
                                            }
                                        }
                                        String[] ary = list.toArray(new String[list.size()]);
                                        float a[] = new float[ary.length];
                                        for (int x = 0; x < ary.length; x++) {
                                            a[x] = Float.parseFloat(ary[x]);
                                        }

                                        barEntries.add(new BarEntry(i, a));
                                        float sum = 0;
                                        for (int z = 0; z < a.length; z++) {
                                            sum = sum + a[z];
                                        }
                                        lineEntry.add(new Entry(i, sum));
                                    }
                                }

                                //柱状图数据
                                BarDataSet barDataSet = new BarDataSet(barEntries, "");
                                barDataSet.setColors(Color.parseColor("#ae312e"), Color.parseColor("#2a3f4e"), Color.parseColor("#579199")
                                        , Color.parseColor("#be765d"), Color.parseColor("#82b49e"));
                                BarData barData = new BarData();
                                barData.addDataSet(barDataSet);
                                //折线图数据
                                LineDataSet lineDataSet = new LineDataSet(lineEntry, "总和");
                                lineDataSet.setColor(Color.parseColor("#b71c1c"));
                                lineDataSet.setCircleColor(Color.parseColor("#b71c1c"));
                                lineDataSet.setValueTextColor(Color.parseColor("#f44336"));
                                lineDataSet.setLineWidth(3f);
                                lineDataSet.setHighlightEnabled(false);
                                LineData lineData = new LineData();
                                lineData.addDataSet(lineDataSet);
                                //组和图数据
                                CombinedData combinedData = new CombinedData(); // 创建组合图的数据源
                                combinedData.setData(barData);  // 添加柱形图数据源
                                combinedData.setData(lineData); // 添加折线图数据源
                                combineChart.setData(combinedData); // 为组合图设置数据源
                                combineChart.invalidate();
                            } catch (JSONException e1) {
                                e1.printStackTrace();
                            }
                        }
                    });

                } else {
                    ToastUtils.showShort(response.message());
                }
            }
        });
    }

}

然后偶然会出现闪退的现象报错信息如下有大神帮忙看下吗感激不尽

loge报错信息

07-17 16:03:30.221 13514-13514/com.chenxi.bimquanziguankong E/CrashHandler: In thread: Thread[main,5,main]
    UncaughtException detected: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.github.mikephil.charting.data.DataSet.getEntryForIndex(DataSet.java:286)
        at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:184)
        at com.github.mikephil.charting.renderer.LineChartRenderer.drawValues(LineChartRenderer.java:547)
        at com.github.mikephil.charting.renderer.CombinedChartRenderer.drawValues(CombinedChartRenderer.java:96)
        at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:264)
        at android.view.View.draw(View.java:15231)
        at android.view.View.updateDisplayListIfDirty(View.java:14167)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.updateDisplayListIfDirty(View.java:14162)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.draw(View.java:15234)
        at android.widget.FrameLayout.draw(FrameLayout.java:598)
        at android.view.View.updateDisplayListIfDirty(View.java:14167)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.updateDisplayListIfDirty(View.java:14162)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.draw(View.java:15234)
        at android.view.View.updateDisplayListIfDirty(View.java:14167)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2554)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2376)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2006)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1085)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5909)
        at android.view.C
07-17 16:03:30.247 13514-13514/com.chenxi.bimquanziguankong E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.chenxi.bimquanziguankong, PID: 13514
    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.github.mikephil.charting.data.DataSet.getEntryForIndex(DataSet.java:286)
        at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:184)
        at com.github.mikephil.charting.renderer.LineChartRenderer.drawValues(LineChartRenderer.java:547)
        at com.github.mikephil.charting.renderer.CombinedChartRenderer.drawValues(CombinedChartRenderer.java:96)
        at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:264)
        at android.view.View.draw(View.java:15231)
        at android.view.View.updateDisplayListIfDirty(View.java:14167)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.updateDisplayListIfDirty(View.java:14162)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.draw(View.java:15234)
        at android.widget.FrameLayout.draw(FrameLayout.java:598)
        at android.view.View.updateDisplayListIfDirty(View.java:14167)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.updateDisplayListIfDirty(View.java:14162)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.View.draw(View.java:14959)
        at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
        at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
        at android.view.View.draw(View.java:15234)
        at android.view.View.updateDisplayListIfDirty(View.java:14167)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
        at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
        at android.view.View.updateDisplayListIfDirty(View.java:14127)
        at android.view.View.getDisplayList(View.java:14189)
        at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
        at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
        at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
        at android.view.ViewRootImpl.draw(ViewRootImpl.java:2554)
        at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2376)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2006)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1085)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5909)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • sxt_zls 狗屎糖 2019-07-17 16:37

    BarLineChartBase.onDraw(BarLineChartBase.java:264) 这一行是啥

    图片说明

    点赞 评论 复制链接分享

相关推荐