Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error inflating WebView XML

I found a crash report on Crashlytics in a Samsung SM G920F (Galaxy S6), version 5.1.1 (on all other devices I didn't retrieve same error).

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.myactivity}: 
android.view.InflateException: Binary XML file line #21: Error inflating class android.webkit.WebView

This is the layout of my webview (the style is an empty style):

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_webview_id"
    style="@style/MyEmptyStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" />

This is how I include it in activity layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/my_webview_layout"
        android:visibility="gone"/>
</LinearLayout>

The error reported is an XML error so I provided all code (simplified). This is a single report so it happened only one time on this device, can someone explain me why?

EDIT (complete stacktrace)

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mypackage/com.mypackage.myactivity}: android.view.InflateException: Binary XML file line #21: Error inflating class android.webkit.WebView
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
       at android.app.ActivityThread.access$1000(ActivityThread.java:198)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6837)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class android.webkit.WebView
       at android.view.LayoutInflater.createView(LayoutInflater.java:640)
       at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
       at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
       at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
       at com.mypackage.drawer.fragments.Myfragment.createContentView(Myfragment.java:50)
       at com.externalpackage.app.ExternalFragment.onCreateView(ExternalFragment.java:115)
       at com.externalpackage.app.LocationExternalFragment.onCreateView(LocationExternalFragment.java:65)
       at com.mypackage.drawer.fragments.Myfragment.onCreateView(Myfragment.java:55)
       at android.app.Fragment.performCreateView(Fragment.java:2114)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1064)
       at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
       at android.app.Activity.performCreateCommon(Activity.java:6492)
       at android.app.Activity.performCreate(Activity.java:6521)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
       at android.app.ActivityThread.access$1000(ActivityThread.java:198)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6837)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.reflect.InvocationTargetException
       at java.lang.reflect.Constructor.newInstance(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
       at android.view.LayoutInflater.createView(LayoutInflater.java:614)
       at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
       at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
       at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
       at com.mypackage.drawer.fragments.Myfragment.createContentView(Myfragment.java:50)
       at com.externalpackage.app.ExternalFragment.onCreateView(ExternalFragment.java:115)
       at com.externalpackage.app.LocationExternalFragment.onCreateView(LocationExternalFragment.java:65)
       at com.mypackage.drawer.fragments.Myfragment.onCreateView(Myfragment.java:55)
       at android.app.Fragment.performCreateView(Fragment.java:2114)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1064)
       at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
       at android.app.Activity.performCreateCommon(Activity.java:6492)
       at android.app.Activity.performCreate(Activity.java:6521)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
       at android.app.ActivityThread.access$1000(ActivityThread.java:198)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6837)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.util.AndroidRuntimeException: android.content.pm.PackageManager$NameNotFoundException: com.google.android.webview
       at android.webkit.WebViewFactory.getFactoryClass(WebViewFactory.java:174)
       at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:109)
       at android.webkit.WebView.getFactory(WebView.java:2202)
       at android.webkit.WebView.ensureProviderCreated(WebView.java:2197)
       at android.webkit.WebView.setOverScrollMode(WebView.java:2256)
       at android.view.View.<init>(View.java:3843)
       at android.view.View.<init>(View.java:3954)
       at android.view.ViewGroup.<init>(ViewGroup.java:513)
       at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:55)
       at android.webkit.WebView.<init>(WebView.java:553)
       at android.webkit.WebView.<init>(WebView.java:498)
       at android.webkit.WebView.<init>(WebView.java:481)
       at android.webkit.WebView.<init>(WebView.java:468)
       at java.lang.reflect.Constructor.newInstance(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
       at android.view.LayoutInflater.createView(LayoutInflater.java:614)
       at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
       at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
       at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
       at com.mypackage.drawer.fragments.Myfragment.createContentView(Myfragment.java:50)
       at com.externalpackage.app.ExternalFragment.onCreateView(ExternalFragment.java:115)
       at com.externalpackage.app.LocationExternalFragment.onCreateView(LocationExternalFragment.java:65)
       at com.mypackage.drawer.fragments.Myfragment.onCreateView(Myfragment.java:55)
       at android.app.Fragment.performCreateView(Fragment.java:2114)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1064)
       at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
       at android.app.Activity.performCreateCommon(Activity.java:6492)
       at android.app.Activity.performCreate(Activity.java:6521)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
       at android.app.ActivityThread.access$1000(ActivityThread.java:198)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6837)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: android.content.pm.PackageManager$NameNotFoundException: com.google.android.webview
       at android.app.ApplicationPackageManager.getPackageInfo(ApplicationPackageManager.java:167)
       at android.webkit.WebViewFactory.getFactoryClass(WebViewFactory.java:146)
       at android.webkit.WebViewFactory.getProvider(WebViewFactory.java:109)
       at android.webkit.WebView.getFactory(WebView.java:2202)
       at android.webkit.WebView.ensureProviderCreated(WebView.java:2197)
       at android.webkit.WebView.setOverScrollMode(WebView.java:2256)
       at android.view.View.<init>(View.java:3843)
       at android.view.View.<init>(View.java:3954)
       at android.view.ViewGroup.<init>(ViewGroup.java:513)
       at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:55)
       at android.webkit.WebView.<init>(WebView.java:553)
       at android.webkit.WebView.<init>(WebView.java:498)
       at android.webkit.WebView.<init>(WebView.java:481)
       at android.webkit.WebView.<init>(WebView.java:468)
       at java.lang.reflect.Constructor.newInstance(Constructor.java)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
       at android.view.LayoutInflater.createView(LayoutInflater.java:614)
       at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
       at android.view.LayoutInflater.onCreateView(LayoutInflater.java:689)
       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:748)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
       at android.view.LayoutInflater.parseInclude(LayoutInflater.java:933)
       at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
       at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
       at com.mypackage.drawer.fragments.Myfragment.createContentView(Myfragment.java:50)
       at com.externalpackage.app.ExternalFragment.onCreateView(ExternalFragment.java:115)
       at com.externalpackage.app.LocationExternalFragment.onCreateView(LocationExternalFragment.java:65)
       at com.mypackage.drawer.fragments.Myfragment.onCreateView(Myfragment.java:55)
       at android.app.Fragment.performCreateView(Fragment.java:2114)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
       at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1064)
       at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884)
       at android.app.Activity.performCreateCommon(Activity.java:6492)
       at android.app.Activity.performCreate(Activity.java:6521)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
       at android.app.ActivityThread.access$1000(ActivityThread.java:198)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:6837)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
like image 887
Giorgio Antonioli Avatar asked Jul 30 '15 08:07

Giorgio Antonioli


4 Answers

It seems that something strange ocurred when the app was inflating the WebView, could be whatever.

For the NameNotFoundException: com.google.android.webview I can deduce that could be a problem with the Google packages (The user could be using a custom ROM and not having them installed.)

If you had more info about the terminal with the error I'll say you go for it, but you have to see the proportion of users/users_with_this_error that you have. Then you could see if it's useful for you work in this issue or not.

From breakline's comment: Check out this post, looks kinda like the same issue: NameNotFoundException webview

like image 128
arodriguezdonaire Avatar answered Nov 14 '22 22:11

arodriguezdonaire


I got the same error on my crashlytics.

After some searches on Google, it seems that this can happen when the user has no webview installed on the phone.

That sounds weird as the webview is a system package, but there is a bug where it is actually possible when the webview package is being updated while the user is using the app. It does not work on all phones, so it can be reproduced on some specific devices (I guess Samsung ones mostly...), but not on others.

I do not remember exactly the step do reproduce it, but something like:

  • start the app
  • go to play store and uninstall the web view updates
  • start to install the updates and switch back to the app
  • on some devices, at the end of the update, the app restart and it works well. On some, the bug appears and if you try to load the webview, it crashes.

Not sure how to deal with it. I guess you can look whether the package exists or register to be notified on package updates to reload the app. In my case, it is really a corner case and happened once or twice, I do not think it requires to work on fixing the issue. If it happens more often, maybe some actions are necessary.

like image 36
Simon Ninon Avatar answered Nov 14 '22 23:11

Simon Ninon


As @simon-ninon said, this is the way I implented in my app.

In the onCreate of the activity that inflates Webview in the UI, you can have a try-catch to save yourself from the errors producing in the Crashlytics.

  /**
     * This element touches the UI elements
     * of the application and set them up.
     */
   try {
        setContentView(R.layout.activity_main);
        // ... further setup
    } catch (Exception ex) {
        transferToNoPackageFoundActivity(ex.getMessage());
    }


    private void transferToNoPackageFoundActivity(String errorMessage) {
        Intent intent = new Intent(this, NoPackageFoundActivity.class);
        intent.putExtra(Intent.EXTRA_INTENT, errorMessage);
        startActivity(intent);
        finish();
    }

In the NoPackageFoundActivity, you can take the user to the Playstore for downloading the webview package.

    /**
 * This field is used for presenting the user
 * with an error image for no connectivity.
 */
private lateinit var mErrorImage: ImageView

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_no_network)
    // get the intent for the error message, if any.
    val intent: Intent = intent
    if (intent.hasExtra(Intent.EXTRA_INTENT)) {
        println(intent.getStringExtra(Intent.EXTRA_INTENT))
    }

    mErrorImage = findViewById(R.id.error_image)
    mErrorImage.contentDescription = resources.getString(R.string.no_network_image_desc)
    Glide.with(this)
            .load(R.drawable.ic_no_web_view_illustration)
            .into(mErrorImage)

    val enableButton = findViewById<Button>(R.id.error_handler_btn)
    enableButton.setOnClickListener { openGooglePlayStore() }
    enableButton.text = resources.getString(R.string.no_package_btn_text)

    val reloadText = findViewById<TextView>(R.id.no_network_reload)
    reloadText.setOnClickListener { reloadApplication() }

    val errorHeadingText = findViewById<TextView>(R.id.error_heading)
    val errorSubheadingText = findViewById<TextView>(R.id.error_subheading)
    errorHeadingText.text = resources.getString(R.string.no_package_heading_text)
    errorSubheadingText.text = resources.getString(R.string.no_package_subheading_text)
}

/**
 * This is a onClickListener Handler that
 * reloads the application when the user
 * clicks on it.
 * It starts the activity again from
 */
private fun reloadApplication() {
    val intent = Intent(this, SplashScreen::class.java)
    startActivity(intent)
    finish()
}

private fun openGooglePlayStore() {
    // you can also use BuildConfig.APPLICATION_ID
    val appId = "com.google.android.webview"
    val rateIntent = Intent(Intent.ACTION_VIEW,
            Uri.parse("market://details?id=$appId"))
    var marketFound = false
    // find all applications able to handle our rateIntent
    val otherApps = packageManager
            .queryIntentActivities(rateIntent, 0)
    for (otherApp in otherApps) { // look for Google Play application
        if (otherApp.activityInfo.applicationInfo.packageName
                == "com.android.vending") {
            val otherAppActivity = otherApp.activityInfo
            val componentName = ComponentName(
                    otherAppActivity.applicationInfo.packageName,
                    otherAppActivity.name
            )
            // make sure it does NOT open in the stack of your activity
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            // task reparenting if needed
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
            // if the Google Play was already open in a search result
            //  this make sure it still go to the app page you requested
            rateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            // this make sure only the Google Play app is allowed to
            // intercept the intent
            rateIntent.component = componentName
            startActivity(rateIntent)
            marketFound = true
            break
        }
    }
    // if GP not present on device, open web browser
    if (!marketFound) {
        val webIntent = Intent(Intent.ACTION_VIEW,
                Uri.parse("https://play.google.com/store/apps/details?id=$appId"))
        startActivity(webIntent)
    }
}
like image 2
Dhruvam Sharma Avatar answered Nov 14 '22 23:11

Dhruvam Sharma


This crash problem happens on Android 5.0 and 5.1 Lollipop (API 21/22), when using version 1.1.0 of some AndroidX Jetpack libraries:

implementation "androidx.appcompat:appcompat:1.1.0"

or

implementation "androidx.preference:preference:1.1.0"

It happens when your device does not have Google Chrome browser or "Android System WebView" installed from the Google Play Store. Some stock emulators that don't have Google Play Services installed will therefore experience this problem.

How to Solve

Updating to a higher AndroidX version, like 1.2.0-alpha03 will fix it, as mentioned here:

Error inflating class android.webkit.WebView Crash on Lollipop when opening PDF

implementation "androidx.appcompat:appcompat:1.2.0-alpha03"

Or use a custom fix in AppCompatActivity.applyOverrideConfiguration(). See: android.view.InflateException Error inflating class android.webkit.WebView

See also:

  • WebView Crash on Android 5-5.1 (API 21-22) Resources$NotFoundException: String resource ID #0x2040002
  • https://issuetracker.google.com/issues/141132133 -- "webview crashes with appcompat1.1.0"
like image 1
Mr-IDE Avatar answered Nov 14 '22 22:11

Mr-IDE