FATAL EXCEPTION: main Process: com.example.ecg_lead, PID: 25186 java.lang.IndexOutOfBoundsException: Index: 0, Size: 1 at java.util.ArrayList.get(ArrayList.java:437) at com.github.mikephil.charting.renderer.LegendRenderer.renderLegend(LegendRenderer.java:377) at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:281) at android.view.View.draw(View.java:19384) at android.view.View.updateDisplayListIfDirty(View.java:18320) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4249) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4229) at android.view.View.updateDisplayListIfDirty(View.java:18279) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4249) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4229) at android.view.View.updateDisplayListIfDirty(View.java:18279) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4249) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4229) at android.view.View.updateDisplayListIfDirty(View.java:18279) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4249) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4229) at android.view.View.updateDisplayListIfDirty(View.java:18279) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4249) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4229) at android.view.View.updateDisplayListIfDirty(View.java:18279) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4249) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4229) at android.view.View.updateDisplayListIfDirty(View.java:18279) at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4249) at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4229) at android.view.View.updateDisplayListIfDirty(View.java:18279) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:710) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:716) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:830) at android.view.ViewRootImpl.draw(ViewRootImpl.java:3254) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3052) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2580) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1555) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7460) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1041) at android.view.Choreographer.doCallbacks(Choreographer.java:847) at android.view.Choreographer.doFrame(Choreographer.java:774) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1027) at android.os.Handler.handleCallback(Handler.java:809) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7555) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)

I face this error while running

public void onMessage(String s) {
            //List<Double> ecgData = new ArrayList<>();
            try {
                // Convert the received value to a float
                float value = Float.parseFloat(s);

                // Apply Butterworth filter to incoming data
                Butterworth butterworth = new Butterworth();
                float filteredValue = (float) butterworth.filter(value);

                // Get the chart's existing data and dataset
                LineData data = chart.getData();
                if (data != null) {
                    LineDataSet set = (LineDataSet) data.getDataSetByIndex(0);
                    if (set == null) {
                        set = createSet();
                        data.addDataSet(set);
                    }
                    // Add the new value to the dataset and notify the chart of the update
                    set.addEntry(new Entry(set.getEntryCount(), filteredValue));
                    data.notifyDataChanged();
                    chart.setDrawMarkers(true);
                    chart.notifyDataSetChanged();
                    // Scroll the chart to the right to show the latest data
                    chart.setVisibleXRangeMaximum(200);
                    chart.moveViewToX(data.getEntryCount() - 1);

                    // Save the ECG data to a CSV file with timestamp and creating a double list

                    try {
                        ecgData.add((double) filteredValue);
                        FileWriter writer = new FileWriter(getExternalFilesDir(null) + "/ecg_data.csv", true);
                        String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
                        int count = set.getEntryCount();
                        if (count <= 3000) { // 3000 samples = 30 seconds assuming 100 Hz sampling rate
                            String csvString = timestamp + "," + filteredValue + "\n";
                            writer.write(csvString);
                        }
                        writer.flush();
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    // Add the timestamped value to the list of ecg data
                    //EcgData ecgData = EcgData.getInstance();
                    //ecgData.add(new EntryWithTimestamp(filteredValue, System.currentTimeMillis()));
                }
            } catch (NumberFormatException | IndexOutOfBoundsException e) {
                e.printStackTrace();
                // Handle the exception
            }
        }


        // Helper method to create a LineDataSet
        public LineDataSet createSet() {
            LineDataSet set = new LineDataSet(null, "ECG");
            set.setAxisDependency(YAxis.AxisDependency.LEFT);
            set.setColor(Color.RED);
            set.setDrawCircles(false);
            set.setDrawValues(false);
            set.setLineWidth(2f);
            set.setHighlightEnabled(false);
            set.setDrawHorizontalHighlightIndicator(false);
            set.setFillAlpha(65);
            set.setFillColor(Color.RED);
            return set;
        }

while running this code,

want to plot the data realtime. But now the app keep crashing

0

There are 0 best solutions below