1

我正在扩展片段,我得到这个错误说,该方法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

4 回答 4

5

您以错误的方式查找视图,view object在 findViewById 时追加如下:

 progressBar = (ProgressBar)myFragmentView.findViewById(R.id.progressBar);
 final WebView mWebView = (WebView)myFragmentView.findViewById(R.id.Browsery);
于 2013-02-20T07:29:01.670 回答
0

你应该这样做。

ImageView imageView = (ImageView) getView().findViewById(R.id.XYZ);
于 2013-02-20T07:27:29.033 回答
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);
}

});
于 2013-02-20T07:29:47.543 回答
0

myFragmentView你在得到你的progressBarand之前就回来了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;
}
于 2013-02-20T08:03:57.063 回答