Android Webview应用程序打开时显示黑屏。

huangapple go评论94阅读模式
英文:

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  = &quot;&lt;iframe width=\&quot;560\&quot; height=\&quot;315\&quot; src=\&quot;https://www.youtube.com/embed/eGPbD-f0b_A\&quot; frameborder=\&quot;0\&quot; allow=\&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\&quot; allowfullscreen&gt;&lt;/iframe&gt;&quot;;
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, &quot;text/html&quot;, &quot;utf-8&quot;);
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  = &quot;&lt;iframe width=\&quot;560\&quot; height=\&quot;315\&quot; src=\&quot;https://www.youtube.com/embed/eGPbD-f0b_A\&quot; frameborder=\&quot;0\&quot; allow=\&quot;accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\&quot; allowfullscreen&gt;&lt;/iframe&gt;&quot;;
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, &quot;text/html&quot;, &quot;utf-8&quot;);
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.

huangapple
  • 本文由 发表于 2020年3月15日 07:20:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/60688240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定