【问题标题】:Android Webview doesn't load HTML sometimesAndroid Webview 有时不加载 HTML
【发布时间】:2016-05-07 15:35:31
【问题描述】:

我正在尝试在我的 web 视图中加载本地 HTML 内容。但有时它无法加载内容,而是显示空白屏幕。每加载 5 次就会发生一次。

注意 我要加载的 HTML 内容是 Official 2048 Source code

下面是我的Activity源代码

public class GameActivity extends AppCompatActivity {

private WebView mWebView;

@SuppressWarnings("ConstantConditions")
@SuppressLint({ "SetJavaScriptEnabled", "NewApi", "ShowToast"})
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
            WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

    setContentView(R.layout.activity_game);

    Toolbar toolbar = (Toolbar) findViewById(R.id.game_toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    // Load webview with game
    mWebView = (WebView) findViewById(R.id.mainWebView);
    WebSettings settings = mWebView.getSettings();
    String packageName = getPackageName();
    settings.setJavaScriptEnabled(true);
    settings.setDomStorageEnabled(true);
    settings.setDatabaseEnabled(true);
    settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    if (Build.VERSION.SDK_INT >= 19) {
        // chromium, enable hardware acceleration
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        // older android version, disable hardware acceleration
        mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    // Since the database path is automatically handled by Chromium Webkit,
    // we should not mention the db path for greater than KitKat version
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        settings.setDatabasePath("/data/data/" + packageName + "/databases");
    }
    mWebView.addJavascriptInterface(new WebInterface2048(this), "Android");
    // If there is a previous instance restore it in the webview
    if (savedInstanceState != null) {
        mWebView.restoreState(savedInstanceState);
    } else {
        mWebView.loadUrl("file:///android_asset/2048/index.html");
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

public class WebInterface2048 {
    Context mContext;

    public WebInterface2048(Context context) {
        mContext = context;
    }

    @JavascriptInterface
    public void showToast(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}
}

到目前为止,我已经尝试了以下方法来解决问题:

  1. 将硬件加速放在清单文件中。
  2. 在 Activity 中启用和禁用硬件加速。
  3. 在 shouldOverrideUrlLoading 中重新加载了相同的 URL
  4. 试图在 onStart() 而不是 onCreate() 中加载 URL

但似乎没有什么对我有用。

我的日志:

D/OpenGLRenderer: endAllActiveAnimators on 0xb7d7e248 (RippleDrawable) with handle 0xb76b0cf0
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor
W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
I/cr_Ime: ImeThread is not enabled.
W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 18631
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow
D/OpenGLRenderer: endAllActiveAnimators on 0xb7a893f8 (RippleDrawable) with handle 0xb7ec8810
I/AppCompatViewInflater: app:theme is now deprecated. Please move to using android:theme instead.
D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor
W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false
I/cr_Ime: ImeThread is not enabled.
W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 18631
D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: true
D/cr_Ime: [InputMethodManagerWrapper.java:68] hideSoftInputFromWindow

附加信息:我的设备是 Moto G3 (Android 6.0.1)

【问题讨论】:

  • 确定一下,您的 webview 是否在宽度和高度上都设置了 match_parent?
  • @mmark,是的,它确实有 match_parent。

标签: javascript android html cordova webview


【解决方案1】:

一直不明白为什么会这样。但作为一种解决方法,我使用以下代码解决了这个问题:

mWebView.postDelayed(new Runnable() {

        @Override
        public void run() {
            mWebView.loadUrl("file:///android_asset/2048/index.html");
        }
    }, 500);

而不是在主线程中加载 URL,如果我在不同的线程中运行,它对我有用。

上述解决方法可能对那些面临问题的人有所帮助,他们的 web 视图在第一次加载时显示为空白,而在重新加载时,它会加载内容。

可能不是 prob=per 解决方案,但它确实有效。如果有人找到更好的解决方案,请发帖。

【讨论】:

    【解决方案2】:

    我通过在 onCreate 方法中手动将 WebView 添加到布局中解决了这个问题:

            LinearLayout ll = findViewById(R.id.content);
            WebView help = new WebView(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            help.setLayoutParams(lp);
            ll.addView(help);
    

    然后照常加载html:

            try {
                 String base64 = Base64.encodeToString(sampleHtml.getBytes("UTF-8"), Base64.DEFAULT);
                 help.loadData(base64, "text/html; charset=utf-8", "base64");
             } catch (UnsupportedEncodingException e) {
                 e.printStackTrace();
             } catch (Exception ex){
                 ex.printStackTrace();
             }
    

    【讨论】:

      【解决方案3】:

      尝试将此与其余代码隔离开来。自己放到oncreate中进行测试。 saveinstancestate 并不总是空的,所以它不会加载。 mWebView.loadUrl("file:///android_asset/2048/index.html");

      【讨论】:

      • 它已经完成并尝试过了。但是这个解决方案也行不通。
      • 试试这个 我搜索了 BindingManager: Cannot call deniedVisibility() - 从来没有看到 pid: 18631 的连接,因为它在你的日志中出现了两次。我在这个网站上找到了几个关于这个的链接,其中一个提到了一个空白屏幕。 stackoverflow.com/questions/30875099/…
      • 我根本没有使用 jQuery。所以没有必要更新库。
      猜你喜欢
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多