我想創建一個使用BARCHART但MPAndroidChart我收到以下錯誤java.lang.ClassCastException: com.github.mikephil.charting.data.Entry cannot be cast to com.github.mikephil.charting.data.BarEntry
MPAndroidChart項不能被轉換爲BarEntry
我第一次從一個不同的活動獲取數據並使用它。
第一項活動:
ArrayList<BarEntry> temperature = new ArrayList<>();
for (int i = 0; i < temp.length(); i++) {
float temp_data = Float.parseFloat(temp.getJSONObject(i).getString("value"));
float humid_data = Float.parseFloat(humid.getJSONObject(i).getString("value"));
float time_data = Float.parseFloat(time.getJSONObject(i).getString("time"));
temperature.add(new BarEntry(time_data, temp_data));
humidity.add(new BarEntry(time_data, humid_data));
}
Intent intent = new Intent(itemView.getContext(), DeviceDataReport.class);
intent.putExtra("temperatureData", temperature);
//put other extras
context.startActivity(intent);
在DeviceDataReport:
ArrayList<BarEntry> temperature = new ArrayList<>();
if (extras != null) {
temperature = extras.getParcelableArrayList("temperatureData");
//get other data
}
temperatureChart = (BarChart) findViewById(R.id.chart_temp);
BarDataSet set1;
if (temperatureChart.getData() != null &&
temperatureChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) temperatureChart.getData().getDataSetByIndex(0);
set1.setValues(temperature);
temperatureChart.getData().notifyDataChanged();
temperatureChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(temperature, "The year 2017"); //this is where error occurs
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
temperatureChart.setData(data);
}
我看不出我只使用Entry
,而不是BarEntry
任何地方。我的xml也說BarChart不是LineChart。
感謝您接受@生命! –