Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android WebView从另一个类加载片段中的url_Android_Android Fragments_Webview_Android Jsinterface - Fatal编程技术网

Android WebView从另一个类加载片段中的url

Android WebView从另一个类加载片段中的url,android,android-fragments,webview,android-jsinterface,Android,Android Fragments,Webview,Android Jsinterface,我想调用从其他类加载片段类中的URL的方法,但我得到的web视图值为null。下面是我的代码 public class WebViewFragment extends Fragment {

我想调用从其他类加载片段类中的URL的方法,但我得到的web视图值为null。下面是我的代码

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

}
      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);
                 }
             });
       }
  }           
从下面的类中,我调用loadURL方法,但为此,我得到了webview的null实例

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

}
      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);
                 }
             });
       }
  }           
新建WebViewFragment()
正在使您的webview为空。相反,您可以使静态方法
loadURL
类似于:

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

}
      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);
                 }
             });
       }
  }           
 public static void loadURL(String url)
    {
        webViewSite.loadUrl(url, Configuration.appRequestHeaders);
    }
把它叫做-

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

}
      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);
                 }
             });
       }
  }           
WebViewFragment.loadURL(Configuration.MOBILE_SITE_URL);