2014-08-31 113 views
0

我想在webview下打开url。我有下面的代码:webview不显示正确

public class FragmentWebView extends Fragment { 
    TextView webview_bar_title; 
    ImageButton menu; 
    WebView wv; 

    public FragmentWebView() { 
    } 

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

     View view = inflater.inflate(R.layout.fragment_webview, container, 
       false); 

     menu = (ImageButton) view.findViewById(R.id.webview_menu); 
     menu.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ((MainActivity) getActivity()).onBackPressed(); 
      } 
     }); 

     webview_bar_title = (TextView) view.findViewById(R.id.webview_share); 
     webview_bar_title.setText("Share"); 

     wv = (WebView) view.findViewById(R.id.fragment_webview); 
     wv.getSettings().setJavaScriptEnabled(true); 
     wv.loadUrl("http://www.google.com"); 

     return view; 
    } 
    } 

而且这里是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/webview_actionbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <ImageButton 
      android:id="@+id/webview_menu" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/arrow_left" /> 

     <TextView 
      android:id="@+id/webview_share" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="18sp" /> 
    </RelativeLayout> 

    <WebView 
     android:id="@+id/fragment_webview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/webview_actionbar" /> 

</RelativeLayout> 

我想在我的WebView加载谷歌网页。但它不起作用,并给我错误,如网站不可用。 Google网页可能会暂时关闭,或者它可能已永久移动到新地址。和建议?提前致谢。

+0

你有连接到互联网? – 2014-08-31 01:22:14

+0

是的,我喜欢。这是模拟器的问题吗? – Shumin 2014-08-31 01:27:11

+0

尝试使用仿真器中的默认网络浏览互联网,并参阅 – 2014-08-31 01:28:30

回答

0

确保您为您的WebView设置WebViewClient,并且您在清单中声明了INTERNET权限。

设置客户端:

wv.setWebViewClient(new WebViewClient()); 

要在清单申报权限:

<uses-permission android:name="android.permission.INTERNET" /> 

这些都是我看到你的代码唯一缺少的东西。

此外,更常见的做法是子类WebViewClient来处理不同的页面事件,而不是使用默认的客户端对象。

+0

谢谢。它的工作原理! – Shumin 2014-08-31 05:05:05