1

I tried to fix this for the past 3 days. I have no idea what to do anymore. The most common thing I found is to sort the data which didn't work out for me.

var valuesTemperature: ArrayList<Entry> = ArrayList()

for (i in 0 until 30) {
        val `val` = (Math.random() * 50).toFloat() - 30
        valuesTemperature.add(Entry(i.toFloat(), `val`, resources.getDrawable((R.drawable.ic_baseline_pause_24))))
        valuesTemperature.sortedWith(compareBy { it.x })
    }

if (chart.data != null &&
            chart.data.dataSetCount > 0) {
        set1 = chart.data.getDataSetByIndex(0) as LineDataSet
        set1.values = valuesTemperature
        set1.notifyDataSetChanged()
        chart.data.notifyDataChanged()
        chart.notifyDataSetChanged()
    } else {
        // create a dataset and give it a type
        set1 = LineDataSet(valuesTemperature, "Temperature")
        set1.setDrawIcons(false)
        set1.mode = LineDataSet.Mode.CUBIC_BEZIER
        
        val dataSets: ArrayList<ILineDataSet> = ArrayList()
        dataSets.add(set1) // add the data sets

        // create a data object with the data sets
        val data = LineData(dataSets)

        // set data
        chart.data = data
}

I always get this error:

 Process: com.example.example, PID: 27169
java.lang.NegativeArraySizeException: -56
    at com.github.mikephil.charting.utils.Transformer.generateTransformedValuesLine(Transformer.java:178)
    at com.github.mikephil.charting.renderer.LineChartRenderer.drawValues(LineChartRenderer.java:549)
    at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:278)
    at android.view.View.draw(View.java:22350)
    at android.view.View.updateDisplayListIfDirty(View.java:21226)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at androidx.constraintlayout.widget.ConstraintLayout.dispatchDraw(ConstraintLayout.java:1975)
    at android.view.View.updateDisplayListIfDirty(View.java:21217)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.draw(View.java:22353)
    at android.view.View.updateDisplayListIfDirty(View.java:21226)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at androidx.coordinatorlayout.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1277)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.updateDisplayListIfDirty(View.java:21217)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.updateDisplayListIfDirty(View.java:21217)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.updateDisplayListIfDirty(View.java:21217)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.updateDisplayListIfDirty(View.java:21217)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.updateDisplayListIfDirty(View.java:21217)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.updateDisplayListIfDirty(View.java:21217)
    at android.view.View.draw(View.java:22081)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4516)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4277)
    at android.view.View.draw(View.java:22353)
    at com.android.internal.policy.DecorView.draw(DecorView.java:806)
    at android.view.View.updateDisplayListIfDirty(View.java:21226)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:559)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:565)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:642)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:4106)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3833)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3104)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1948)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8177)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
    at android.view.Choreographer.doCallbacks(Choreographer.java:796)
    at android.view.Choreographer.doFrame(Choreographer.java:731)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7660)
2021-04-28 16:13:14.320 27169-27169/com.example.exampleE/AndroidRuntime:     at 
java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

I have absolutely no idea what to do anymore... I seems like there are a lot of possible fixes for Java, but I haven't managed to translate any of these to Kotlin successfully.

1 Answer 1

1

Answer may be late for OP but can help someone else facing this issue. Faced it my self today and this question did not have the answer for me.

So after I started to look into it, I found two cases that could cause this-

Case 1 : If your points repeat (x axis value should not repeat)

var values = arrayListOf<Entry>() 
values.add(Entry(1f, 50f))        
values.add(Entry(1f, 100f))       
values.add(Entry(2f, 80f))        
values.add(Entry(2f, 70f))        
values.add(Entry(4f, 90f)) 
values.add(Entry(4f, 90f))

Case 2 : If your set of entries looks like below where x is jumbled up and you need to sort it out your self based on 'x' values in Entry list;

var values = arrayListOf<Entry>() 
values.add(Entry(4f, 50f))        
values.add(Entry(5f, 100f))       
values.add(Entry(2f, 80f))        
values.add(Entry(3f, 70f))        
values.add(Entry(1f, 90f)) 

If your data set looks like below you are good;

var values = arrayListOf<Entry>() 
values.add(Entry(1f, 50f))        
values.add(Entry(2f, 100f))       
values.add(Entry(3f, 80f))        
values.add(Entry(4f, 70f))        
values.add(Entry(5f, 90f))        
values.add(Entry(6f, 50f))        
values.add(Entry(7f, 100f))       
values.add(Entry(8f, 80f))        
values.add(Entry(9f, 70f))  

  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.