Android如何使用片段显示网络视图
我正在更新我的一些旧项目,并使用片段来更新事物的外观。我试图使用一个片段来启动一个webview,但当我尝试运行它时,我的logcat中出现了以下错误Android如何使用片段显示网络视图,android,webview,null,fragment,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的
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中发生的自己的活动,但我希望在片段中有一个小视图,然后当我想使用该类时,我会通过intent启动它,并将我需要的任何内容传递给webview,如intent中的url和其他额外参数。我试着在一个片段中建立一个网络视图,但我还没有让它工作。这是我目前使用的代码
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在一个我可以在视图中使用的片段中打开呢。你考虑过扩展吗?我一分钟前才发现……我会在这里发帖,告诉你我最终如何让它工作。我想我可能会同意我在这里的链接上找到的。我无法让webviewfragment正常工作,文档站点上除了方法列表之外没有太多文档。请注意,这个答案来自2013年,文档中说,“这个类在API级别28中被弃用。”