Android-如何使用片段显示webview

时间:2012-02-19 21:10:51

标签: android webview null fragment

我正在更新一些旧项目并使用片段来更新事物的外观。我尝试使用片段来启动webview但是当我尝试运行它时,我的logcat中出现以下错误。

 E/Web Console(22464): Uncaught TypeError: Cannot read property 'addEventListener' of null at    
 http://m.yahoo.com/?.tsrc=yahoo&mobile_view_default=true:1

我以前使用webview的方法是创建一个类,它是在webview中发生的自己的活动但是我想在一个片段中有一个小视图然后当我想使用类我将通过意图启动它并将我需要的任何内容传递给webview,就像url和intent中的其他参数一样。我尝试在片段中设置一个webview,但我还没有让它工作。这是我目前使用的代码。

    public class WebViewer extends Fragment {
WebView Wv;
String url = "http://www.yahoo.com";
Activity act;
public WebViewer(){}



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.act = activity;
}

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

    View mView = inflater.inflate(R.layout.webview, container);

      Wv = (WebView) mView.findViewById(R.id.webview1);
      Wv.getSettings().setJavaScriptEnabled(true);
      Wv.getSettings().setRenderPriority(RenderPriority.HIGH);
      Wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
      Wv.getSettings().setDomStorageEnabled(true);
      Wv.setWebViewClient(new HelloWebViewClient());
      Wv.getSettings().setBuiltInZoomControls(true);
      Wv.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress)
                {
                    act.setTitle("Loading...");
                    act.setProgress(progress * 100);             
                    if(progress == 100)
                        getActivity().setTitle(R.string.app_name);
                }
            });
      Wv.loadUrl(url);

    return mView;

}
}

然后这是使用此片段的活动的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/bggreydotted"
 >

<fragment 
    android:id="@+id/webFragment"
    android:layout_width="150dip"
    android:layout_height="match_parent"
    android:name="my.proj.WebViewer"></fragment>


</LinearLayout>

那么如何才能在我可以在视图中使用的片段中打开webview。

1 个答案:

答案 0 :(得分:2)

您是否考虑过延长WebViewFragment