簡體   English   中英

對於Fragment02類型,未定義方法findViewById()

[英]The method findViewById() is undefined for the type Fragment02

嘗試以通常的方式創建WebView時出現錯誤,說

對於類型Frament02,未定義方法findViewById()

怎么會? 我讀過我可以使用該方法引用父活動,但不確定我的ActivitiesFragment是否正在使用它?

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

或者,您可以只在onCreateViewonActivityCreated return View ,使用getView().findViewById()初始化視圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM