Error inflating WebView XML

前端 未结 7 1525
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-04 00:10

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.Runtim         


        
相关标签:
7条回答
  • 2021-01-04 00:42

    It seems that something strange occurred when the app was inflating the WebView, could be whatever. For the NameNotFoundException: com.google.android.webview problem with the Google packages

     if(getPackageManager().hasSystemFeature("android.software.webview") == true && isPackageExisted("com.google.android.webview")) {
                    if (Constant.isNetworkConnected(LoginActivity.this)) {
    
                         //Your Intent 
                    } else {
                        Toast.makeText(getApplicationContext(), resources.getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
                    }
                }else
                    {
                        Constant.showDialog(LoginActivity.this,"Please install the webview");
                    }
            }
    

    //Make method for package check

    public boolean isPackageExisted(String targetPackage){
        List<ApplicationInfo> packages;
        PackageManager pm;
    
        pm = getPackageManager();
        packages = pm.getInstalledApplications(0);
        for (ApplicationInfo packageInfo : packages) {
            if(packageInfo.packageName.equals(targetPackage))
                return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2021-01-04 00:43

    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

    0 讨论(0)
  • 2021-01-04 00:44

    I had same problem and i solved this problem by changing its theme in manifest

    0 讨论(0)
  • 2021-01-04 00:45

    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)
        }
    }
    
    0 讨论(0)
  • 2021-01-04 00:46

    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"
    0 讨论(0)
  • 2021-01-04 00:49

    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.

    0 讨论(0)
提交回复
热议问题