android - FindViewById returns null on custom relative layout -
in init method, findviewbyid returns null on r.id.disable_view_content.
public class disableview extends relativelayout { private view content; private view disableview; private int disablebackgroundcolor; private boolean isenabled; public disableview(context context) { super(context); init(); } public disableview(context context, attributeset attrs) { super(context, attrs); obtainattributes(context, attrs); init(); } public disableview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); obtainattributes(context, attrs); init(); } @targetapi(build.version_codes.lollipop) public disableview(context context, attributeset attrs, int defstyleattr, int defstyleres) { super(context, attrs, defstyleattr, defstyleres); obtainattributes(context, attrs); init(); } private void obtainattributes(context context, attributeset attributeset) { typedarray typedarray = context.gettheme().obtainstyledattributes(attributeset, r.styleable.disableview, 0, 0); disablebackgroundcolor = typedarray.getcolor(r.styleable.disableview_disablebackground, 0x00000000); isenabled = typedarray.getboolean(r.styleable.disableview_enabled, false); typedarray.recycle(); } private void init() { content = findviewbyid(r.id.disable_view_content); disableview = new view(getcontext()); relativelayout.layoutparams lp = new layoutparams(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.match_parent); lp.addrule(relativelayout.align_top, r.id.disable_view_content); lp.addrule(relativelayout.align_bottom, r.id.disable_view_content); lp.addrule(relativelayout.align_left, r.id.disable_view_content); lp.addrule(relativelayout.align_top, r.id.disable_view_content); disableview.setbackgroundresource(disablebackgroundcolor); disableview.setlayoutparams(lp); setenabled(isenabled); } public void setenabled(boolean enabled) { content.setenabled(enabled); if(enabled) disableview.setvisibility(view.gone); else disableview.setvisibility(view.visible); } }
here's xml
<com....view.common.disableview android:id="@+id/..._disableview_...disable" android:layout_width="match_parent" android:layout_height="wrap_content"> <linearlayout android:id="@id/disable_view_content" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:id="@+id/..._textview_..." android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shape_rect_green" android:gravity="center" android:paddingtop="18dp" android:paddingbottom="18dp" android:layout_marginbottom="12dp" android:textsize="@dimen/size_text" android:text="@string/..." android:textcolor="@android:color/white" android:duplicateparentstate="true"/> </linearlayout> </com....view.common.disableview>
id disable_view_content in attr.xml
<resources> <item type="id" name="disable_view_content"/> </resources>
everything seems fine, view id disable_view_content direct child of disableview. either in edit mode or on app, null pointer because reference content null.
edit: here's stack trace:
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.view.view.setenabled(boolean)' on null object reference @ com....view.common.disableview.setenabled(disableview.java:71) @ com....view.common.disableview.init(disableview.java:67) @ com....view.common.disableview.<init>(disableview.java:35)
you calling findviewbyid()
far in view's lifecycle.
your init()
called directly constructor, , before have created layout own view. @ point in time, view has no layout , has no children either.
the earliest can access child views after 1 of addview()
methods has been called.
Comments
Post a Comment