我面临一个非常奇怪的问题:我试图在片段中加载webview,但我知道我无法找到一种方法让它工作,我绝对有任何关于原因的想法。奇怪的是,如果在Activity
中运行,相同的相同代码也可以工作这是我非常基本的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<WebView
android:id="@+id/webview"
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
这是代码工作的活动
public class ProvaWebview extends Activity {
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prova_webview);
wv=(WebView)findViewById(R.id.webview);
WebSettings settings=wv.getSettings();
wv.setWebChromeClient(new WebChromeClient() {
});
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
settings.setJavaScriptEnabled(true);
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
@Override
public void onBackPressed() {
super.onBackPressed();
wv.clearHistory();
wv.clearCache(true);
wv.loadUrl("about:blank");
wv.pauseTimers(); //new code
wv = null;
}
public String getHTML() {
String html = "<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/hEFqVV5q44E?rel=0&controls=0&showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>";
return html;
}
}
这是代码不起作用的片段
public class FrTabDescriptionWebView extends Fragment {
public static final String TAG = FrTabDescriptionWebView.class.getName();
private WebView wv;
public static FrTabDescriptionWebView newInstance() {
FrTabDescriptionWebView fragment = new FrTabDescriptionWebView();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public FrTabDescriptionWebView() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.activity_prova_webview, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
wv = (WebView) view.findViewById(R.id.webview);
WebSettings settings = wv.getSettings();
wv.setWebChromeClient(new WebChromeClient() {
});
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
settings.setJavaScriptEnabled(true);
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
public String getHTML() {
String html = "<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/hEFqVV5q44E?rel=0&controls=0&showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>";
return html;
}
}
我到底做错了什么?
我还尝试使用WebViewFragment用getWebView替换wv,但问题仍然存在
谢谢你的帮助
答案 0 :(得分:3)
将您的所有代码从onCreated(..)
移至onCreateView(...)
。并删除
super.onCreateView(inflater, container, savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_prova_webview, null);
wv = (WebView) view.findViewById(R.id.webview);
WebSettings settings = wv.getSettings();
wv.setWebChromeClient(new WebChromeClient() {
});
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
settings.setJavaScriptEnabled(true);
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
return view;
}
答案 1 :(得分:0)
我发现了问题:
我有另一个活动显示嵌入了youtube视频的WebView,并在关闭活动时停止我在完成方法中使用此代码
wv.clearHistory();
wv.clearCache(true);
wv.loadUrl("about:blank");
wv.pauseTimers();
wv = null;
避免使用此代码使片段正常工作。我认为导致问题的真正代码是
wv.pauseTimers();
<强>确认强>
经过一些测试,我可以确认真正的问题是
wv.pauseTimers();
删除此行所有内容都按预期工作,使youtube视频停止播放此代码正常
wv.clearHistory();
wv.clearCache(true);
wv.loadUrl("about:blank");
wv = null;