Android: how do I access views (using findViewById) of an Activity from a Fragment onCreateView()? -


i have pretty simple project activity contains fragment. thing may unfamiliar using videoenabledwebview isn't complicated , i've used before. it's custom webview.

here code:

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<!-- don't forget internet permission if using webview load remote content --> <uses-permission android:name="android.permission.internet" />  <application     android:allowbackup="true"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme"     android:hardwareaccelerated="true" > <!-- works in api level 11+, , allows html5 videos play in-line -->      <activity         android:name="name.cpr.exampleactivity"         android:label="@string/app_name" >         <intent-filter>             <action android:name="android.intent.action.main" />             <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity> </application> 

exampleactivity.java

import android.app.activity; import android.os.bundle; import android.util.log;  import cpr.name.videoenabledwebview.r;  public class exampleactivity extends activity {     @override     protected void oncreate(bundle savedinstancestate)     {         log.i("rai", "activity oncreate");         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_example);      }  } 

activity_example.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".exampleactivity">  <!-- view hidden when video goes fullscreen --> <relativelayout     android:id="@+id/nonvideolayout"     android:layout_width="match_parent"     android:layout_height="match_parent" >      <fragment android:name="name.cpr.webviewfragment"         android:id="@+id/webviewfragment"         android:layout_width="match_parent"         android:layout_height="match_parent" />  </relativelayout>  <!-- view video shown when video goes fullscreen --> <relativelayout     android:id="@+id/videolayout"     android:layout_width="match_parent"     android:layout_height="match_parent" >  </relativelayout> 

webviewfragment.java

package name.cpr;  import android.app.activity; import android.app.fragment; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import cpr.name.videoenabledwebview.r;   public class webviewfragment extends fragment {  private videoenabledwebview webview; private videoenabledwebchromeclient webchromeclient; view rootview;  public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {      rootview = inflater.inflate(r.layout.root, container, false);      webview = (videoenabledwebview) rootview.findviewbyid(r.id.webview);      log.i("rai", "nonvideolayout = " + r.id.nonvideolayout);      log.i("rai", "videolayout = " + r.id.videolayout);      view nonvideolayout = rootview.findviewbyid(r.id.nonvideolayout);      if(nonvideolayout == null){          log.i("rai", "why nonvideolayout null?");     }      viewgroup videolayout = (viewgroup)     rootview.findviewbyid(r.id.videolayout);      if(videolayout == null){          log.i("rai", "why videolayout null?");     }      webchromeclient = new videoenabledwebchromeclient(nonvideolayout, videolayout);      webview.setwebchromeclient(webchromeclient);      webview.loadurl("http://m.youtube.com");      return rootview;  } } 

root.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">  <textview     android:layout_width="100dp"     android:layout_height="100dp"     android:text="the fragment"/>  <name.cpr.videoenabledwebview     android:id="@+id/webview"     android:layout_width="fill_parent"     android:layout_height="fill_parent" />  </linearlayout> 

i can't access 2 relative layouts in activity, keep getting null.

things have tried don't work:

rootview.findviewbyid //rootview return inflate

getactivity().findviewbyid

getview().findviewbyid

container.findviewbyid

i've tried in fragments oncreate:

context = getactivity().getapplicationcontext();  webviewfragmentid = context.getresources().getidentifier(         "root", "layout", context.getpackagename());  webviewid = context.getresources().getidentifier("webview", "id",         context.getpackagename());  nonvideolayoutid = context.getresources().getidentifier("nonvideolayout", "id", context.getpackagename()); videolayoutid = context.getresources().getidentifier("videolayout","id", context.getpackagename());  rootview = inflater.inflate(webviewfragmentid, null);  webview = (videoenabledwebview) rootview.findviewbyid(webviewid);  view nonvideolayout = getactivity().findviewbyid(nonvideolayoutid); viewgroup videolayout = (viewgroup) getactivity().findviewbyid(videolayoutid);  webchromeclient = new videoenabledwebchromeclient(nonvideolayout, videolayout);  webview.setwebchromeclient(webchromeclient);  webview.loadurl("http://m.youtube.com");   return rootview; 

you should override onactivitycreated , access views need activity in method because when oncreateview run in fragment activity has not yet had layout set cannot access views.

@override public void onactivitycreated (bundle savedinstancestate) {     view nonvideolayout = getactivity().findviewbyid(r.id.nonvideolayout);     viewgroup videolayout = (viewgroup) getactivity().findviewbyid(r.id.videolayout);     ... } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -