java - getActivity().findViewById(int id) returns null even after fragment has completed building -


i'm following android training in android's website. stuck @ point in fragments. problem can't reference textview in fragment.

textview view = (textview) getactivity().findviewbyid(r.id.article_s); 

i have 2 fragments in 1 activity. 1 plain simple listfragment , other plain simple textview. here fragments , activity layout xml files.

articlefragment:

<!-- fragment_article.xml --> <?xml version="1.0" encoding="utf-8"?> <textview     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="some text"     android:textsize="18sp"     android:id="@+id/article_s"/> 

mainactivity (my container fragments)

<!-- activity_main.xml --> <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent">  <fragment     android:layout_height="match_parent"     android:name="com.example.bora.fragmentsmyway.headlinesfragment"     android:id="@+id/headlines_fragment"     android:layout_weight="1"     android:layout_width="0dp" />  <fragment     android:layout_height="match_parent"     android:name="com.example.bora.fragmentsmyway.articlefragment"     android:layout_weight="2"     android:id="@+id/article_fragment"     android:layout_width="0dp" />  </linearlayout> 

there public method in articlefragment takes string update content of textview of itself:

public void updateview(string article) {         textview view = (textview) getactivity().findviewbyid(r.id.article_s);         try{             view.settext(article);         } catch (nullpointerexception e) {             e.printstacktrace();         }     } 

after views created, call updateview method mainactivity. nullpointerexception when try find article textview. have tried cleaning, changing textview's resource id.

however have found solution problem. don't know why seems work when wrap textview linearlayout in fragment_article.xml:

<linearlayout     xmlns:android="..."     android:layout_width="match_parent"     android:layout_height="match_parent">      <textview         xmlns:android="http://schemas.android.com/apk/res/android"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="some text"         android:textsize="18sp"         android:id="@+id/article_s" />  </linearlayout> 

but wondering this: how can, in tutorial samples, above code work , can't work when wrote? checked every code letter letter there no difference between mine ant theirs. because of versions?

i'm working api 23 marshmallow.

make sure fragment attached activity before calling updateview

this

 textview view = (textview) getactivity().findviewbyid(r.id.article_s); 

will give npe cause not belong activity

the views belong fragments in case

 view view = inflater.inflate(r.layout.fragment_layout, container, false);  textview textview = (textview) view.findviewbyid(r.id.article_s); 

specifically, fragment can access activity instance getactivity() , perform tasks such find view in activity layout:

read communicating activity @ https://developer.android.com/guide/components/fragments.html

so use getactivity find views belong activity not case in code.

edit -

wrapping linearlayout should not make difference viewgroup. if need single textview no need linearlayout.

read accepted answer @ : difference between getactivity() , view in fragment


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -