使用mpandroidchart lib的浮点格式异常无效

时间:2017-04-10 08:21:46

标签: android android-studio android-studio-2.2 android-json android-studio-2.1

我正在使用mpandroidchart lib创建Stacked barchart但我收到错误(无效的浮动格式异常)。

这是我的代码:

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response) {
        try {
            for (int i = 0; i < response.length(); i++) {
                JSONObject object = response.getJSONObject(i);

                String treecondition = object.getString("Description");
                Integer count = (object.getInt("Collection"));
                Integer absent = (object.getInt("FEE"));

                arrayMonth.add(treecondition);
                arrayPresent.add(count);
                arrayAbsent.add(absent);
            }

            ArrayList<String> xVals = new ArrayList<>();
            for (int j = 0; j < arrayMonth.size(); j++)
                xVals.add(String.valueOf(arrayMonth.get(j)));

            float valu1=Float.parseFloat((arrayPresent).toString());
            float value2= Float.parseFloat((arrayAbsent).toString());

            BarEntry entry = new BarEntry(new float[] { valu1, value2 }, 0);

            BarDataSet barDataSet=new BarDataSet((List<BarEntry>) entry,"");

            BarData barData=new BarData(xVals,barDataSet);
            attendancebarchart.setData(barData);
            attendancebarchart.animateY(5000);
            barDataSet.setColor(Color.GREEN);
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

AppController.getInstance().addToRequestQueue(jsonArrayRequest, "test");

1 个答案:

答案 0 :(得分:0)

BarEntry entry = new BarEntry(new float[] { valu1, value2 }, 0);

将此行更改为

BarEntry entry = new BarEntry(0, new float[] { valu1, value2 });

如果查看库代码,您将看到此函数的签名如下:

BarEntry(float x, float[] vals)

另外,请确保这两个值正确

float valu1=Float.parseFloat((arrayPresent).toString());
float value2= Float.parseFloat((arrayAbsent).toString());