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 :
I want to put width of linechart as
LayoutParams.match_parent
but when IDE asks me to import classLayoutParams
, the options I get areRelativeLayout
,FrameLayout
,ViewGroup
and many others but any one among them is not related toLineChart
.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)
- 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