[英]The method findViewById(int) is undefined for the type PostDetail (Fragment)
[英]The method findViewById() is undefined for the type Fragment02
嘗試以通常的方式創建WebView
時出現錯誤,說
對於類型Frament02,未定義方法
findViewById()
怎么會? 我讀過我可以使用該方法引用父活動,但不確定我的Activities
或Fragment
是否正在使用它?
public class Fragment02 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_02, container, false);
WebView myWebView = (WebView) findViewById(R.id.webView1);
嘗試這種方式
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_02, container, false);
WebView myWebView = (WebView) v.findViewById(R.id.webView1);
return v;
}
你應該參考您的Views
,從你的Fragment
充氣視圖。
改成
View view = inflater.inflate(R.layout.fragment_02, container, false);
WebView myWebView = (WebView)view. findViewById(R.id.webView1);
return view.
放大布局。 使用視圖對象初始化fragment_02.xml
視圖。
http://developer.android.com/reference/android/view/View.html#findViewById%28int%29
或者,您可以只在onCreateView
和onActivityCreated
return View
,使用getView().findViewById()
初始化視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.