英文:
Android Webview app shows black screen when i open it
问题
public class MainActivity extends AppCompatActivity {
private WebView zwebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView = (WebView) findViewById(R.id.myWebView);
zwebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
英文:
Android weview app I compiled in andriod studio crashes immediately with a blackscreen when I open it. The app uses an iframe embed url.Compiles fine but crashes with a black screen when I open it.
Why is it crashing?
public class MainActivity extends AppCompatActivity {
private WebView zwebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView = (WebView) findViewById(R.id.myWebView);
zwebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
答案1
得分: 1
以下是已翻译的代码部分:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView zwebView = new WebView(this);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
setContentView(zwebView);
}
}
这是您提供的代码的翻译部分。
英文:
I just copy-paste your code and its working fine the video is loading in the webview so if you face issue can you kindly share the log details that what error you actually getting.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView zwebView = new WebView(this);
String Video;
Video = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/eGPbD-f0b_A\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
WebSettings webSettings = zwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
zwebView.setInitialScale(1);
zwebView.getSettings().setAllowFileAccess(true);
zwebView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
zwebView.setWebChromeClient(new WebChromeClient());
zwebView.getSettings().setJavaScriptEnabled(true);
zwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
zwebView.getSettings().setLoadWithOverviewMode(true);
zwebView.getSettings().setUseWideViewPort(true);
zwebView.getSettings().setAppCacheEnabled(true);
zwebView.loadData(Video, "text/html", "utf-8");
zwebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
});
zwebView.loadUrl(Video);
setContentView(zwebView);
}
}
Here is the code which I use and it's working. not crashing after all.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论