简体   繁体   English

方法findViewById(int)未定义

[英]The method findViewById(int) is undefined

I am extending fragment and i get this error saying, the method findViewById(int) is undefined for the type Song. 我正在扩展fragment,并且收到此错误消息,对于Song类型,方法findViewById(int)未定义。

The error appear for both my progress bar and webview on the findViewById. 对于进度条和webView,在findViewById上均显示该错误。

public class Song extends Fragment{


    ProgressBar progressBar;


    @Override


    public View onCreateView(LayoutInflater inflater, ViewGroup container,


    Bundle savedInstanceState) {


    View myFragmentView = inflater.inflate(R.layout.song, container, false);


    return myFragmentView;


    progressBar = (ProgressBar)findViewById(R.id.progressBar);


    final WebView mWebView = (WebView)findViewById(R.id.Browsery);


    mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);


    mWebView.loadUrl("....");


    mWebView.setWebViewClient(new WebViewClient(){



    public void onPageFinished(WebView view, String url){


    progressBar.setVisibility(View.INVISIBLE);
    }

    });

Please i need help 请我需要帮助

Thank you. 谢谢。

You are finding view in wrong way, append view object while findViewById as like below: 您以错误的方式查找视图,在findViewById时附加view object ,如下所示:

 progressBar = (ProgressBar)myFragmentView.findViewById(R.id.progressBar);
 final WebView mWebView = (WebView)myFragmentView.findViewById(R.id.Browsery);

您应该这样做。

ImageView imageView = (ImageView) getView().findViewById(R.id.XYZ);
public class Song extends Fragment{


ProgressBar progressBar;


@Override


public View onCreateView(LayoutInflater inflater, ViewGroup container,


Bundle savedInstanceState) {


View myFragmentView = inflater.inflate(R.layout.song, container, false);





progressBar = (ProgressBar)myFragmentView.findViewById(R.id.progressBar);


final WebView mWebView = (WebView)myFragmentView.findViewById(R.id.Browsery);
 return myFragmentView;

mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);


mWebView.loadUrl("....");


mWebView.setWebViewClient(new WebViewClient(){



public void onPageFinished(WebView view, String url){


progressBar.setVisibility(View.INVISIBLE);
}

});

you r returning myFragmentView before getting your progressBar and webView . 在获取progressBarwebView之前,先返回myFragmentView your code should be modified as : 您的代码应修改为:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View myFragmentView = inflater.inflate(R.layout.song, container, false);

    progressBar = (ProgressBar)myFragmentView.findViewById(R.id.progressBar);

    final WebView mWebView = (WebView)myFragmentView.findViewById(R.id.Browsery);

     /// your code ....
     /// .......

    // this should be the last line of your onCreateView()
    return myFragmentView;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM