android fragment rootview,Android:如何从Fragment onCreateView()访问Activity的视图(使用findViewById)?...

我有一个非常简单的项目,其中包含一个包含片段的Activity.只有你可能不熟悉的是我使用的是VideoEnabledWebView,这并不是那么复杂,而是我以前用过的东西.这只是一个自定义的webview.

这是我的代码:

AndroidManifest.xml中

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme"

android:hardwareAccelerated="true" >

android:name="name.cpr.ExampleActivity"

android:label="@string/app_name" >

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".ExampleActivity">

android:id="@+id/nonVideoLayout"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:id="@+id/webViewFragment"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android:id="@+id/videoLayout"

android:layout_width="match_parent"

android:layout_height="match_parent" >

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

android:orientation="vertical" android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="100dp"

android:layout_height="100dp"

android:text="the fragment"/>

android:id="@+id/webView"

android:layout_width="fill_parent"

android:layout_height="fill_parent" />

我只是无法访问我的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时,活动尚未设置其布局,因此您无法访问其视图.

@Override

public void onActivityCreated (Bundle savedInstanceState) {

View nonVideoLayout = getActivity().findViewById(R.id.nonVideoLayout);

ViewGroup videoLayout = (ViewGroup) getActivity().findViewById(R.id.videoLayout);

...

}

标签:android,fragment,findviewbyid

来源: https://codeday.me/bug/20190829/1760558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值