您好,并提前感谢您的帮助,
我正在尝试使用ArrayList currentBodyTemp中的信息在此窗口中显示线图,但是当我切换到bodyTempInfo视图时,我收到错误?
出了什么问题?
bodyTempInfo类
public class bodyTempInfo extends AppCompatActivity {
ArrayList<Double> currentBodyTemp;
private int[] arraySize;
private double[] arrayInformation;
private void fillGraph() {
int i = 0;
for(i = 0; i < currentBodyTemp.size(); i++) {
arraySize[i] = i;
arrayInformation[i] = currentBodyTemp.get(i);
}
}
private void displayBodyTemp(String bodyTemp) {
TextView textView = (TextView) findViewById(R.id.bodyTempInfoArray);
textView.setText(bodyTemp);
}
public void displayCurrentBodyTempArray(View view) throws FileNotFoundException {
ArrayList<Double> list = new ArrayList<>();
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
//Read text from file
File file = new File(sdcard, "Download/temp.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
list.add(Double.parseDouble(line));
text.append('\n');
}
br.close();
}
catch(IOException e) {
throw new Error(e);
}
currentBodyTemp = list;
displayBodyTemp("Your Body Temperature Readings are: " + text);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_body_temp_info);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
try {
displayCurrentBodyTempArray(findViewById(R.id.bodyTempInfoArray));
} catch (FileNotFoundException e) {
throw new Error(e);
}
LineChart chart = (LineChart) findViewById(R.id.chart);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
fillGraph();
chart.setDragEnabled(true);
chart.setScaleEnabled(true);
chart.setDrawGridBackground(false);
chart.setPinchZoom(true);
int i = 0;
ArrayList<Entry> vals = new ArrayList<Entry>();
for (i=0; i<currentBodyTemp.size(); i++) {
Entry entry = new Entry(currentBodyTemp.get(i).floatValue(), i);
vals.add(entry);
}
LineDataSet set = new LineDataSet(vals, "Body Temperature");
set.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<LineDataSet> dataSet = new ArrayList<LineDataSet>();
ArrayList<String> xVals = new ArrayList<String>();
dataSet.add(set);
for (i=0; i<currentBodyTemp.size(); i++) {
xVals.add(String.valueOf(i));
}
LineData data = new LineData(xVals, dataSet);
chart.setData(data);
chart.invalidate();
}
}
content_body_temp_info.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
android:layout_margin = "8dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Body Temperature Measurements will go here"
android:id = "@+id/bodyTempInfoArray"
android:layout_weight = "0"/>
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
android:layout_margin = "8dp"/>
</LinearLayout>
错误消息
10-26 13:36:28.535 11615-11615/com.teamfara.circadianrhythmmonitor4 D/AndroidRuntime: Shutting down VM
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: FATAL EXCEPTION: main
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: Process: com.teamfara.circadianrhythmmonitor4, PID: 11615
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.teamfara.circadianrhythmmonitor4/com.teamfara.circadianrhythmmonitor4.bodyTempInfo}: java.lang.ClassCastException: com.github.mikephil.charting.charts.BarChart cannot be cast to com.github.mikephil.charting.charts.LineChart
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.access$1000(ActivityThread.java:198)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: Caused by: java.lang.ClassCastException: com.github.mikephil.charting.charts.BarChart cannot be cast to com.github.mikephil.charting.charts.LineChart
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at com.teamfara.circadianrhythmmonitor4.bodyTempInfo.onCreate(bodyTempInfo.java:102)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6500)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.access$1000(ActivityThread.java:198)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-26 13:36:28.545 11615-11615/com.teamfara.circadianrhythmmonitor4 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
答案 0 :(得分:2)
异常本身有一个解决方案:
Caused by: java.lang.ClassCastException: com.github.mikephil.charting.charts.BarChart cannot be cast to com.github.mikephil.charting.charts.LineChart.
您正在尝试将Barchart转换为LineChart。看看您的布局和活动代码。
在你的布局中:
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight = "1"
android:layout_margin = "8dp"/>
在你的活动中,你试图将BarChart转换为LineChart。
LineChart chart = (LineChart) findViewById(R.id.chart);
替换您的观点或更新您的活动代码。