【问题标题】:The method findViewById(int) is undefinedfindViewById(int) 方法未定义
【发布时间】:2013-02-20 07:24:58
【问题描述】:

我正在扩展片段,我收到此错误消息,方法 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);
    }

    });

我需要帮助

谢谢。

【问题讨论】:

  • 在发布之前进行适当的编辑。
  • 查看 myFragmentView = inflater.inflate(R.layout.song, container, false); progressBar = (ProgressBar)myFragmentView.findViewById(R.id.progressBar);返回我的片段视图;像这样改变你有 2 使用视图。在片段中作为其膨胀布局

标签: android android-fragments fragment android-fragmentactivity


【解决方案1】:

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

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

【讨论】:

  • @ Robinhood 当我将 myFragmentView 添加到第二个时,它显示另一个错误,提示代码无法访问
  • onCreateView内执行视图和点击事件的初始化
  • 感谢您的回复,但我想怎么做呢,我还是个菜鸟
  • 是的!!!!它奏效了,非常感谢 Robinhood 你们这些人,谢谢它就像一种魅力,非常感谢
【解决方案2】:

你应该这样做。

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

【讨论】:

  • @ Rajnikant 当我将 getView() 添加到第二个时,它显示另一个错误,提示代码无法访问 -
【解决方案3】:

您在获得progressBarwebView 之前返回myFragmentView。您的代码应修改为:

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

【讨论】:

    【解决方案4】:
    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);
    }
    
    });
    

    【讨论】:

    • 当我将 myFragmentView 添加到第二个并显示错误提示代码无法访问时
    猜你喜欢
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多