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

这是我做柱状图的代码

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个回答

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

图片说明

sxt_zls
说点儿什么吧~ 回复qq_43544413: xingtao.sun@gmail.com ; BarLineChartBase的onDraw方法
7 个月之前 回复
qq_43544413
qq_43544413 回复都是坑: 不然我把代码发给你你有时间帮我调试一下。你给我个邮箱地址
7 个月之前 回复
qq_43544413
qq_43544413 感觉该加的我都加了
7 个月之前 回复
qq_43544413
qq_43544413 我代码都有贴上去你看是哪里缺了什么
7 个月之前 回复
sxt_zls
说点儿什么吧~ 回复qq_43544413: 索引越界了,你要看你的数据源 和 你绘制的时候 size是否一致 ,猜测原因是你绘制的时候长度数据不足导致的 ,进行非空和size=0的判断
7 个月之前 回复
qq_43544413
qq_43544413 回复都是坑: 主要是这个问题是偶然出现的,debug不好弄啊
7 个月之前 回复
sxt_zls
说点儿什么吧~ 回复qq_43544413: 你这是索引越界了,debug下就找到问题代码了
7 个月之前 回复
sxt_zls
说点儿什么吧~ 回复qq_43544413: 发邮件吧,qq就算了
7 个月之前 回复
qq_43544413
qq_43544413 回复都是坑: 我QQ1762339772
7 个月之前 回复
qq_43544413
qq_43544413 我这个是安卓的代码
7 个月之前 回复
qq_43544413
qq_43544413 回复都是坑: 你方便加我QQ吗
7 个月之前 回复
sxt_zls
说点儿什么吧~ 回复qq_43544413: 这一行都什么代码啊,搞个截图出来吧
7 个月之前 回复
sxt_zls
说点儿什么吧~ 回复qq_43544413: 熟的很 https://blog.csdn.net/sxt_zls/article/details/78782226
7 个月之前 回复
qq_43544413
qq_43544413 你对这个柱状图折线图这种图例的东西熟吗?熟的话帮我看看呢!你说的这行是这个mRenderer.drawValues(canvas);
7 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

3
项目启动不报错,但是访问后报404,求大神支招.
1
请问各位大神我的ARkitRemote在手机上会闪退是怎么回事
1
vs添加luanet.dll报错原因求大神解决?
2
jdbc连接MySQL报错, 有没有大神帮忙解决一下
9
萌新求助Android Studio工程编译没问题,运行app就闪退,我试了别的代码,只要修改了xml文件也会闪退。
2
苹果企业账号的描述文件过期后,APP闪退,请问各位大神有好的解决方案么?
3
关于win10安装VS2015后闪退的问题,求助大神帮忙解答!
1
Django执行python manage.py migrate报错情况如下,求大神指教
2
【没有C币有大神看不】celery一处理任务就报错,报错信息里说的没有反向怎么破?
1
报错Conflicting types for 'login'问题,求大神帮助
1
关于vector用法报错,求大神指教
1
安装npm报错,求大神告知!!!
2
【求助大神】编译没问题,运行的时候却闪退,在线等
1
vba 当触发KillTimer时,excel直接闪退了,连看代码出错的的时间都没有,敢问大神原因?
1
在安装robotframework的AutoItLibrary库的时候遇到一个报错File "setup.py", line 57,求教各位大神
2
MySQL 8 的时间模糊查询报错:> 1525 - Incorrect DATETIME value: '2019-07%' ,求大神解决
2
VS2017+Qt开发环境,编译报错“error LNK2001: 无法解析的外部符号”,请各位大神指导。
1
关于VS2010操作ACCESS2007,连接报错!请大神指教!
4
关于报错闪退的问题?有没有大神来看一眼。。。。
0
关于Cookies的问题有大神来看看吗?