java - Custom View children are null after inflating the view in Android -


i working on android app. have custom view , layout follows:

<com.hello.view.card.inner.simplecardview xmlns:android="http://schemas.android.com/apk/res/android"         android:id="@+id/card_simple_linear_layout"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical" >          <textview             android:id="@+id/simple_label"             android:layout_width="wrap_content"             android:layout_height="wrap_content"/>  </com.hello.view.card.inner.simplecardview> 

and java class:

public class simplecardview extends linearlayout {     protected simplecard card = null;     protected textview textview;      public simplecardview(context context) {         super(context);              init(context);     }      public simplecardview(context context, attributeset attrs) {         super(context, attrs);               init(context);     }      public simplecardview(context context, attributeset attrs, int defstyle) {         super(context, attrs, defstyle);         init(context);     }      protected void init(context context) {         textview = (textview)findviewbyid(r.id.simple_label);     }      public void setcard(simplecard card) {         this.card = card;         textview.settext(card.getmessage());             } } 

and how inflating view (i tried both following calls):

simplecardview view = (simplecardview)inflater.inflate(r.layout.card_simple, null); //simplecardview view = (simplecardview)inflater.inflate(r.layout.card_simple, parent); view.setcard(card); 

the problem having when view.setcard(card) called, see textview null, though expecting set in init(..) method. can tell me not being set correctly? in advance.

thank answers. turns out init(context) should not called in constructor. right place call in onfinishinflate(). following change helped fix it:

public class simplecardview extends linearlayout {     protected simplecard card = null;     protected textview textview;      public simplecardview(context context) {         super(context);          }      public simplecardview(context context, attributeset attrs) {         super(context, attrs);           }      public simplecardview(context context, attributeset attrs, int defstyle) {         super(context, attrs, defstyle);     }      @override     protected void onfinishinflate() {         super.onfinishinflate();         init(getcontext());     }      protected void init(context context) {         textview = (textview)findviewbyid(r.id.simple_label);     }      public void setcard(simplecard card) {         this.card = card;         textview.settext(card.getmessage());             } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -