0

I want to plot 12 line graphs. So I put a linear layout inside a scrollview. I will be populating the linear layout with 12 charts. I want each chart to have height equal to width of the screen(since I am viewing graphs in landscape mode). My code goes like this :

class MainActivity : AppCompatActivity() {

private var screenHeight = 0
private var screenWidth = 0

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_main)

    val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)
    screenWidth = displayMetrics.widthPixels
    screenHeight = displayMetrics.heightPixels
    Log.i("width", screenWidth.toString())
    Log.i("height", screenHeight.toString())

    val chartHeight = screenHeight / 12
    for (i in 0..11){
        val lineChart = LineChart(this)
        lineChart.layoutParams.height = chartHeight  //line 33 giving error
        linearLayout.addView(lineChart)
    }
}
}

I am having the following problems :

  1. I want to put width of linechart as LayoutParams.match_parent but when IDE asks me to import class LayoutParams, the options I get are RelativeLayout, FrameLayout, ViewGroup and many others but any one among them is not related to LineChart.

  2. I referred this but its not working for LineChart. I don't know why. I am getting this error :

AndroidRuntime: FATAL EXCEPTION: main Process: atharva.example.com.graphlist, PID: 29323 java.lang.RuntimeException: Unable to start activity ComponentInfo{atharva.example.com.graphlist/atharva.example.com.graphlist.MainActivity}: java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5476) at android.app.ActivityThread.access$1200(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1827) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7329) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Caused by: java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference at atharva.example.com.graphlist.MainActivity$override.onCreate(MainActivity.kt:33) at atharva.example.com.graphlist.MainActivity$override.access$dispatch(MainActivity.kt) at atharva.example.com.graphlist.MainActivity.onCreate(MainActivity.kt:0) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)

  1. What does error at line 0 (referring to the above error log) mean?

Please help me in setting layout params through kotlin code for custom view like MP Android LineChart

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
thapro
  • 44
  • 11

1 Answers1

1

You didn't set the width value of your chart. Try to create new LayoutParams object. That worked for me.

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams([your_width], [your_height]);
lineChart.setLayoutParams(params);

Hope it helps.

Cagri Yalcin
  • 402
  • 4
  • 13