【问题标题】:Error in Android Studio - "cannot find symbol method findviewbyid(int)" [duplicate]Android Studio 中的错误-“找不到符号方法 findviewbyid(int)”[重复]
【发布时间】:2016-06-19 06:04:42
【问题描述】:

webview 部分有错误。请帮帮我。按照此处的教程,我设置了一个应用程序,它应该能够打开一个 URL 并将其放入 WebView。唯一的问题是这里找不到 findViewById 代码:

//webviewcode
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.webkit.WebSettings;
    import android.webkit.WebView;

    import info.healthazure.materialdesign.R;


    public class MessagesFragment extends Fragment {
        private WebView myWebView;
        public MessagesFragment() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_messages, container, false);
            myWebView = (WebView)findViewById(R.id.webView);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            myWebView.loadUrl("http://www.medplusmart.com/");



            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
        }

        @Override
        public void onDetach() {
            super.onDetach();
        }
    }

对于解决此问题的任何帮助将不胜感激。

【问题讨论】:

    标签: java android android-fragments webview android-webview


    【解决方案1】:

    不要只使用这个findViewById(),而是使用rootView.findViewById(),因为你的整个布局都在rootview中膨胀。

    【讨论】:

      【解决方案2】:

      使用

      myWebView = (WebView)rootView.findViewById(R.id.webView);
      

      而不是

      myWebView = (WebView)findViewById(R.id.webView);
      

      【讨论】:

        猜你喜欢
        • 2016-11-10
        • 1970-01-01
        • 2017-04-27
        • 2018-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-16
        • 1970-01-01
        相关资源
        最近更新 更多