我想从其他类调用用于在Fragment类中加载URL的方法,但是我正在获取Web视图的空值。下面是我的代码。

public class WebViewFragment extends Fragment
{
     String url;
     Context context;
     public WebView webViewSite;

     public WebViewFragment()
     {
     }
     public void init(String currentURL, Context context1)
     {
         url = currentURL;
         context = context1;
     }

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

         View view = inflater.inflate(R.layout.activity_webview_site, container, false);
         webViewSite = (WebView)view.findViewById(R.id.webViewSite);
         webViewSite.getSettings().setJavaScriptEnabled(true);
         webViewSite.addJavascriptInterface(new JsInterface(context),"AndroidJSObject");
         webViewSite.setWebViewClient(new SwAWebClient());
         webViewSite.setWebChromeClient(new SwAWebChromeClient());
         loadURL(Configuration.MOBILE_SITE_URL);
         return view;
    }
    public  void loadURL(String url)
    {
        webViewSite.loadUrl(url, Configuration.appRequestHeaders);
    }

}


从下面的类中,我正在调用loadURL方法,但是为此,我正在获取Webview的null实例。

      public class JsInterface
      {
           Handler myHandler;
           private Context mContext;
           public JsInterface(Context c)
           {
               mContext = c;
               myHandler = new Handler();
           }
           @JavascriptInterface
           public void loadFromBaseUrl()
           {
              myHandler.post(new Runnable()
              {
                 @Override
                 public void run()
                 {
                     Toast.makeText(mContext,"Please Wait",Toast.LENGTH_SHORT).show();
                     new      WebViewFragment().loadURL(Configuration.MOBILE_SITE_URL);
                 }
             });
       }
  }

最佳答案

new WebViewFragment()使您的Web视图为空。相反,您可以使静态方法loadURL像这样:

 public static void loadURL(String url)
    {
        webViewSite.loadUrl(url, Configuration.appRequestHeaders);
    }


并称其为-

WebViewFragment.loadURL(Configuration.MOBILE_SITE_URL);

10-05 23:57