加载WebView时遇libEGL: validate_display:255 error 3008(EGL_BAD_DISPLAY)求助
First, let's start with your formatted WebViewActivity code for easy reference:
public class WebviewActivity extends Activity { WebView webView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_webview); webView = (WebView) findViewById(R.id.webview); setupWebView(); webView.loadUrl("http://www.Example.com/"); } private void setupWebView() { webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)"); super.onPageFinished(view, url); } }); webView.addJavascriptInterface(this, "MyApp"); } @JavascriptInterface public void resize(final float height) { WebviewActivity.this.runOnUiThread(new Runnable() { @Override public void run() { webView.setLayoutParams(new RelativeLayout.LayoutParams( getResources().getDisplayMetrics().widthPixels, (int) (height * getResources().getDisplayMetrics().density) )); } }); } }
That libEGL: validate_display:255 error 3008 (EGL_BAD_DISPLAY) usually ties into conflicts with hardware acceleration, rendering contexts, or how you're updating the WebView's layout. Let's walk through actionable fixes:
1. Disable Hardware Acceleration for the WebView
This is the most common fix for this specific EGL error. Hardware acceleration can sometimes clash with WebView's rendering pipeline. Try adding this line in your setupWebView() method:
private void setupWebView() { webView.getSettings().setJavaScriptEnabled(true); // Turn off hardware acceleration for the WebView webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // Rest of your setup code... }
Alternatively, you can disable it for the entire activity in your AndroidManifest.xml:
<activity android:name=".WebviewActivity" android:hardwareAccelerated="false"/>
Note: This might slightly impact WebView performance, but it's a quick test to confirm if the EGL error is acceleration-related.
2. Tweak Your Layout Update Logic
Your current resize() method replaces the WebView's LayoutParams entirely, which can disrupt the rendering context mid-process. Instead, modify the existing params and trigger a layout refresh:
@JavascriptInterface public void resize(final float height) { WebviewActivity.this.runOnUiThread(new Runnable() { @Override public void run() { ViewGroup.LayoutParams currentParams = webView.getLayoutParams(); currentParams.width = getResources().getDisplayMetrics().widthPixels; currentParams.height = (int) (height * getResources().getDisplayMetrics().density); webView.setLayoutParams(currentParams); // Force the layout to update properly webView.requestLayout(); } }); }
Also, double-check that your WebView's parent layout is actually a RelativeLayout—if it's a different type (like LinearLayout), you'll need to use the corresponding LayoutParams class instead.
3. Delay JS Execution in onPageFinished
Running JavaScript immediately when onPageFinished fires can sometimes interfere with the WebView's final rendering steps. Try adding a small delay:
@Override public void onPageFinished(WebView view, String url) { view.postDelayed(() -> { webView.loadUrl("javascript:MyApp.resize(document.body.getBoundingClientRect().height)"); }, 100); // Wait 100ms before running the JS super.onPageFinished(view, url); }
This gives the WebView a moment to finish setting up its rendering context before you trigger the resize.
4. Update the System WebView
Occasionally, this error is caused by bugs in older versions of Android's System WebView. Head to the Google Play Store on your test device, search for "Android System WebView", and make sure it's updated to the latest version. You can also test on different Android versions to see if the issue is specific to an OS release.
5. Adjust Page Loading Timing
Try moving the loadUrl() call to the onResume() method instead of onCreate() to ensure the WebView is fully initialized before loading content:
@Override protected void onResume() { super.onResume(); if (webView != null && !"http://www.Example.com/".equals(webView.getUrl())) { webView.loadUrl("http://www.Example.com/"); } }
Start with disabling hardware acceleration first—it's the quickest way to diagnose if that's the root cause. If that fixes it, you can decide whether to keep it disabled or try the other tweaks to preserve performance.
内容的提问来源于stack exchange,提问作者Riya

