java - context is null in custom view -
i'm working on custom view extends horizontalscrollview
, i'm doing following code:
public bottomnavigation(context context) { super(context, null, 0); } public bottomnavigation(context context, attributeset attrs) { super(context, attrs, 0); } public bottomnavigation(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); this.context = context; } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); createitems(); } public void createitems() { if (items < 4) { return; } height = getheight(); width = getwidth(); paddingtop = height / 7; itemsheight = height - paddingtop; itemswidth = width / 4; requestlayout(); views.clear(); removeallviews(); linearlayout linearlayout = new linearlayout(context); linearlayout.setorientation(linearlayout.horizontal); linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(height, viewgroup.layoutparams.wrap_content); linearlayout.setlayoutparams(layoutparams); addview(linearlayout); for(int i=0;i<items;i++) { linearlayout linearlayout1 = new linearlayout(context); linearlayout.layoutparams layoutparams1 = new linearlayout.layoutparams(height, itemswidth); linearlayout1.setlayoutparams(layoutparams1); linearlayout1.setorientation(linearlayout.vertical); } }
but i'm getting error when running application:
java.lang.runtimeexception: unable start activity componentinfo{com.example.amir_p.bottomnavigation3/com.example.amir_p.bottomnavigation3.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources android.content.context.getresources()' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2450) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2510) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1363) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5459) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources android.content.context.getresources()' on null object reference @ android.view.viewconfiguration.get(viewconfiguration.java:366) @ android.view.view.<init>(view.java:3794) @ android.view.view.<init>(view.java:3898) @ android.view.viewgroup.<init>(viewgroup.java:573) @ android.widget.linearlayout.<init>(linearlayout.java:203) @ android.widget.linearlayout.<init>(linearlayout.java:199) @ android.widget.linearlayout.<init>(linearlayout.java:195) @ android.widget.linearlayout.<init>(linearlayout.java:191) @ com.example.amir_p.bottomnavigation3.bottomnavigation.createitems(bottomnavigation.java:68) @ com.example.amir_p.bottomnavigation3.bottomnavigation.setitems(bottomnavigation.java:108)
after debugging noticed context null why? problem?
you initialize context
member variable in 1 of 3 constructors , apparently either 1 of other 2 invoked.
in custom view, don't need store context
reference yourself. call getcontext()
whenever need context
.
Comments
Post a Comment