有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java WebView膨胀异常

在我的活动中,我添加了Webview(没有异常):

<WebView
        tools:context=".Activities.WebViewActivity"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:id="@+id/webView"/>

在我的WebViewActivity中,我声明:

webview = findViewById(R.id.webView);
webview.setWebViewClient(new AppWebViewClients());
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setDefaultTextEncodingName("utf-8");

问题是我的应用程序在一些安卓设备上崩溃(在Crashlytics中看到过)。。。我在emulator上重现了这种情况,并获得了此日志(第一部分):

java.lang.RuntimeException: Unable to start activity ComponentInfo{jamhome.ru.lktsj/ru.domyland.superdom.Activities.WebViewActivity}: 安卓.view.InflateException: Binary XML file line #12: Error inflating class 安卓.webkit.WebView
        at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at 安卓.app.ActivityThread.access$800(ActivityThread.java:144)
        at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at 安卓.os.Handler.dispatchMessage(Handler.java:102)
        at 安卓.os.Looper.loop(Looper.java:135)
        at 安卓.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我找到了很好的解决方案-创建类并使用它来代替基本Webview:

public class LollipopFixedWebView extends WebView {

    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing);
    }

    public static Context getFixedContext(Context context) {
        return context.createConfigurationContext(new Configuration());
    }
}

好的,没有更多的崩溃,很好,但是:我加载了包含html中的Select标记的页面,如果点击它就不起作用了!基本上,安卓必须用Select的变量打开系统对话框,但它不

有什么想法吗


共 (2) 个答案

  1. # 1 楼答案

    简单地做:

    改变

    <WebView
            tools:context=".Activities.WebViewActivity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView"/>
    

    致-:

    <yourpackgename.LollipopFixedWebView
            tools:context=".Activities.WebViewActivity"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView"/>
    

    以及java文件中的更改

    LollipopFixedWebView webview=findViewById(R.id.webview);
    
  2. # 2 楼答案

    HERE查看我的答案

    简而言之:仅在默认Context导致InflateException的操作系统版本上使用固定Context

    private static Context getFixedContext(Context context) {
        if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) // Android Lollipop 5.0 & 5.1
            return context.createConfigurationContext(new Configuration());
        return context;
    }