Im更新我的一些旧项目并使用片段更新事物的外观。我试图用一个片段来启动一个webview,但是当我尝试运行它时,我在我的logcat中出现以下错误。Android-How to use a fragment to display a webview
E/Web Console(22464): Uncaught TypeError: Cannot read property 'addEventListener' of null at
http://m.yahoo.com/?.tsrc=yahoo&mobile_view_default=true:1
我用网页视图的方法是只创建一个类,这是它自己的活动发生在网页视图,但我想有一个片段内的小图,然后当我想使用类我会通过intent启动它,并将任何我需要的东西传递给webview,就像url和其他参数在intent中的extras一样。我试图在一个片段中设置一个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打开我可以在视图中使用的片段。
http://developer.android.com/reference/android/webkit/WebViewFragment.html我刚刚发现,一分钟前.....生病后回到这里与我如何得到它的工作当我最终这样做的时候。 – 2012-02-19 21:52:41
http://stackoverflow.com/questions/9161192/webviewfragment-webview-is-null-after-doing-a-fragmenttransaction我想我可以跟我在这里找到的链接去。我无法让webviewfragment工作,并且除了方法列表之外,在文档网站上没有太多关于它的文档。 – 2012-02-19 23:51:33