我想加载列表视图中列出的网址,然后点击列表项,点击将这些网址发送到另一个片段中,该片段基本上包含一个webview来加载该网址。所以,我想将网址加载到该webview中。它打印所有日志,但不显示任何内容。我试过的是:
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//url = getArguments().getString("url");
url = "http://www.google.com";
browser = (WebView) getActivity().findViewById(R.id.webView_fullNews);
// initialize web settings
settings = browser.getSettings();
settings.setJavaScriptEnabled(true);
settings.setLoadsImagesAutomatically(true);
settings.setSupportMultipleWindows(true);
settings.setAppCacheEnabled(true);
settings.setAppCachePath( getActivity().getApplicationContext().getCacheDir().getAbsolutePath() );
//settings.setAllowFileAccess(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
// dialog to show error
final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
// loading dialog
final ProgressDialog progress = new ProgressDialog( getActivity() );
progress.setMessage("Loading News...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
browser.setWebChromeClient(new WebChromeClient(){});
browser.setWebViewClient(new WebViewClient(){
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progress.isShowing()) {
progress.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.e(TAG, "Error: " + description);
Toast.makeText(getActivity(), "Ops! " + description, Toast.LENGTH_SHORT).show();
alertDialog.setTitle("Error");
alertDialog.setMessage(description);
alertDialog.setButton(Button.TEXT_ALIGNMENT_CENTER, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alertDialog.show();
}
});
browser.loadUrl(String.valueOf(Uri.parse(url)));
}
xml内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/webView_fullNews"/>
</LinearLayout>
答案 0 :(得分:0)
代码应该在
中 public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
browser = (WebView) rootView.findViewById(R.id.webView_fullNews);
//Your webview settings
//here
return rootView;
}
片段的方法,您需要从xml初始化和膨胀片段的视图,然后设置webview的设置属性。