
我有一个非常简单的项目,其中包含一个包含片段的Activity.只有你可能不熟悉的是我使用的是VideoEnabledWebView,这并不是那么复杂,而是我以前用过的东西.这只是一个自定义的webvIEw.
这是我的代码:
AndroIDManifest.xml中
<?xml version="1.0" enCoding="utf-8"?><!-- Don't forget the internet permission if you will be using the WebVIEw to 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" > <!-- Only works in API level 11+, and allows the HTML5 vIDeos to 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 that will be 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 where the vIDeo will be 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 is nonVIDeolayout null?"); } VIEwGroup vIDeolayout = (VIEwGroup) rootVIEw.findVIEwByID(R.ID.vIDeolayout); if(vIDeolayout == null){ Log.i("rai", "why is 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>我只是无法访问我的Activity中的那两个相对布局,我一直得到null.
我尝试过的东西不起作用:
rootVIEw.findVIEwByID // rootVIEw是来自inflate的返回
getActivity().findVIEwByID
getVIEw().findVIEwByID
container.findVIEwByID
我也在我的碎片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;解决方法:
您应该覆盖onActivityCreated并从该方法中的活动访问所需的视图,因为当在片段中运行onCreateVIEw时,活动尚未设置其布局,因此您无法访问其视图.
@OverrIDepublic voID onActivityCreated (Bundle savedInstanceState) { VIEw nonVIDeolayout = getActivity().findVIEwByID(R.ID.nonVIDeolayout); VIEwGroup vIDeolayout = (VIEwGroup) getActivity().findVIEwByID(R.ID.vIDeolayout); ...} 总结 以上是内存溢出为你收集整理的Android:如何从Fragment onCreateView()访问Activity的视图(使用findViewById)?全部内容,希望文章能够帮你解决Android:如何从Fragment onCreateView()访问Activity的视图(使用findViewById)?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)