java - android findViewById is null in LinearLayout -


android findviewbyid method return value null

this view custom class

view.xml

<include         android:id="@+id/view_before_login"         layout="@layout/view_mypage_before_login"         android:layout_width="match_parent"         android:layout_height="wrap_content"/> 

view activity java (fragment)

@override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         final viewgroup rootview = (viewgroup) inflater.inflate(r.layout.view, container, false);          mbefore = (memberbeforeloginview)rootview.findviewbyid(r.id.view_before_login);         mafter = (memberafterloginview)rootview.findviewbyid(r.id.view_after_login);          mbefore.setstatelistener(this);         mafter.setstatelistener(this);          route();          return rootview;     } 

inside xml view_mypage_before_login.xml

<?xml version="1.0" encoding="utf-8"?> <com.main.view.memberafterloginview     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:orientation="vertical" android:layout_width="match_parent"     android:layout_height="match_parent">     <imagebutton         android:id="@+id/profile"         android:layout_width="137dp"         android:layout_height="121dp"         app:srccompat="@drawable/empty" />      <button         android:id="@+id/logout"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="logout" /> </com.main.view.memberafterloginview> 

com.main.view.memberafterloginview.java

public class memberafterloginview extends linearlayout implements view.onclicklistener, memberviewlistener.memberchildviewinterface {     private memberviewlistener mmemberlistener = null;      private button mbtnlogout = null;      public memberafterloginview(context context) {         super(context);         initialize(context);     }      public memberafterloginview(context context, @nullable attributeset attrs) {         super(context, attrs);         initialize(context);     }      public memberafterloginview(context context, @nullable attributeset attrs, int defstyleattr) {         super(context, attrs, defstyleattr);         initialize(context);     }      private void initialize(context context) {         /*layoutinflater inflater = (layoutinflater) context                 .getsystemservice(context.layout_inflater_service);         view v = inflater.inflate(r.layout.view_mypage_after_login, null);         */          mbtnlogout = (button) findviewbyid(r.id.logout);     } ... 

why null variable mbtnlogout ? (initialize function inside variable)

i immature in english. sorry.

private view initialize(context context) {     layoutinflater inflater = (layoutinflater) context             .getsystemservice(context.layout_inflater_service);     view v = inflater.inflate(r.layout.view_mypage_after_login, null);      mbtnlogout = (button) v.findviewbyid(r.id.logout);     return v; } 

the button part of new inflated view. since inflate result not applied activity yet activity.findviewbyid cannot find it.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -