findViewById(int)方法未定义

时间:2013-02-20 07:24:58

标签: android android-fragments fragment android-fragmentactivity

我正在扩展片段,我得到这个错误说,方法findViewById(int)未定义类型Song。

findViewById上的进度条和webview都出现错误。

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);
    }

    });

我需要帮助

谢谢。

4 个答案:

答案 0 :(得分:5)

您正在以错误的方式查找视图,请在findViewById下添加view object,如下所示:

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

答案 1 :(得分:0)

你应该这样做。

ImageView imageView = (ImageView) getView().findViewById(R.id.XYZ);

答案 2 :(得分:0)

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);
}

});

答案 3 :(得分:0)

在获得myFragmentViewprogressBar之前,您将返回webView。您的代码应修改为:

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;
}