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