Android app programming- not sure why findViewByID does the following: -


this code works:

public static class placeholderfragment extends fragment {      public placeholderfragment() {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {          view rootview = inflater.inflate(r.layout.fragment_detail, container, false);         intent intent = getactivity().getintent();         string weather = intent.getstringextra(intent.extra_text);         if(weather != null) {             textview textviewweather=(textview)rootview.findviewbyid(r.id.weathertext);             textviewweather.settext(weather); 

but if replace, in line

(textview)rootview.findviewbyid(r.id.weathertext);

rootview findview() or getactivity, returns null next line creates run time fatal error halts program.

i don't understand why. thought context activity?! i'm confused,and can't find answer via android developer,so here am...

thanks in advance whoever helps!!

this line gets element id weathertext rootview. nothing fragment xml file

textview textviewweather=(textview)rootview.findviewbyid(r.id.weathertext); 


if replacing above code below

textview textviewweather=(textview)getactivity().findviewbyid(r.id.weathertext); 

now parent activity in fragment hosted has textview , id weathertext searched framework.

obvious not present @ all.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -