简体   繁体   English

由 android.view.InflateException 引起

[英]Caused by android.view.InflateException

Hello there i have a problem which just happened in some devices.您好,我有一个问题刚刚发生在某些设备上。 App is completely working but i am getting crash that i can't find any solution in stackoverflow.应用程序完全正常工作,但我崩溃了,我在 stackoverflow 中找不到任何解决方案。

'Caused by android.view.InflateException Binary XML file line #18: Binary XML file line #18: Error inflating class ImageView' "Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{com. . /com. . .ui.layout.modules.intro.activities.ActivityIntro}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class ImageView" '由 android.view.InflateException 二进制 XML 文件行 #18 引起:二进制 XML 文件行 #18:错误膨胀 class ImageView'“致命异常 java.lang.Runtime 无法启动 activity.lang.Info.Exception 无法启动ui.layout.modules.intro.activities.ActivityIntro}:android.view.InflateException:二进制 XML 文件行 #18:二进制 XML 文件行 #18:膨胀 class ImageView 时出错”

I researched solutions and mostly they are about the drawable file or xxhdpi, xhdpi and etc.我研究了解决方案,主要是关于可绘制文件或 xxhdpi、xhdpi 等。 在此处输入图像描述

Our folder structure is not a problem in this situation.在这种情况下,我们的文件夹结构不是问题。

Here is layout xml code:这是布局 xml 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/fz_background"
android:orientation="vertical">

<LinearLayout
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="12dp"
    android:gravity="center"
    android:id="@+id/lnrSelectLanguage"
    android:layout_width="50dp"
    android:layout_height="40dp"
    android:padding="8dp">

    <ImageView
        android:id="@+id/imgSelectLanguage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/lang_en_icon"
        android:background="@color/transparent"
        android:scaleType="centerCrop" />

</LinearLayout>

<LinearLayout
    android:id="@+id/lnrUpper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginTop="22dp"
    android:layout_marginEnd="40dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:includeFontPadding="false"
            android:text="@string/onboarding_page_main_title"
            android:textAppearance="@style/F20SB"
            android:textColor="@color/fz_dark_blue"
            android:transitionName="txtDigitalAssistantTransition" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:background="@drawable/onboard_title_background"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingTop="4dp"
            android:paddingBottom="16dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:includeFontPadding="false"
                android:paddingStart="24dp"
                android:paddingEnd="24dp"
                android:text="@string/app_name"
                android:textAppearance="@style/F24SB"
                android:textColor="@color/fz_dark_blue"
                android:transitionName="txtFizTransition" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:src="@drawable/onboard_title_stroke_property"
                android:transitionName="imgFizImageTransition" />

        </LinearLayout>

    </LinearLayout>


</LinearLayout>

<LinearLayout
    android:layout_below="@id/lnrUpper"
    android:layout_above="@id/relativeBottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_marginStart="40dp"
        android:layout_marginEnd="40dp"
        tools:itemCount="1"
        tools:listitem="@layout/layout_info_item"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/fz_background" />

</LinearLayout>

<RelativeLayout
    android:layout_alignParentBottom="true"
    android:id="@+id/relativeBottom"
    android:layout_width="match_parent"
    android:layout_height="190dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/onboard_bottom_background" />

    <LinearLayout
        android:layout_above="@id/lnrBottom"
        android:layout_width="match_parent"
        android:layout_height="12dp"
        android:gravity="center"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/lnrIndicatorOne"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:background="@drawable/onboard_item_not_selected_background"
            android:orientation="horizontal" />

        <LinearLayout
            android:id="@+id/lnrIndicatorTwo"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_marginStart="18dp"
            android:background="@drawable/onboard_item_not_selected_background"
            android:orientation="horizontal" />

        <LinearLayout
            android:id="@+id/lnrIndicatorThree"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_marginStart="18dp"
            android:background="@drawable/onboard_item_not_selected_background"
            android:orientation="horizontal" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/lnrBottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/lnrLoginButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="22dp"
            android:layout_marginEnd="32dp"
            android:background="@color/fz_background"
            android:orientation="vertical">

            <Button
                android:id="@+id/btnLogin"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:background="@drawable/new_button_save_background"
                android:clickable="true"
                android:focusable="true"
                android:includeFontPadding="false"
                android:text="@string/onboarding_page_login"
                android:textAppearance="@style/F17SB"
                android:textColor="@color/fz_background" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/lnrRegisterButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="18dp"
            android:layout_marginEnd="32dp"
            android:layout_marginBottom="27dp"
            android:background="@color/fz_background"
            android:orientation="vertical">

            <Button
                android:id="@+id/btnRegister"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:background="@drawable/button_background_blue_border"
                android:clickable="true"
                android:focusable="true"
                android:includeFontPadding="false"
                android:text="@string/onboarding_page_register"
                android:textAppearance="@style/F17SB"
                android:textColor="@color/fz_dark_blue" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

line of 18 which is ImageView is giving error on devices:第 18 行 ImageView 在设备上出现错误:

Nexus 5x versin (8.1.0) and Redmi K20 android version (11) Nexus 5x 版本 (8.1.0) 和 Redmi K20 android 版本 (11)

The code of Activity Intro:活动介绍代码:

class ActivityIntro :
BaseActivityViewBinding<LayoutIntroSlidePageBinding>(LayoutIntroSlidePageBinding::inflate) {

private lateinit var introAdapter: IntroAdapter

override fun loadUI() {

    LanguageHelper.updateLanguage(this)

    super.loadUI()

    changeStatusBar(R.color.fz_background)

    initializeAdapter()

    binding.btnLogin.setOnClickListener { changePage(ActivityLogin::class.java) }

    binding.btnRegister.setOnClickListener { changePage(ActivityRegisterFizbot::class.java) }

    binding.lnrSelectLanguage.setOnClickListener { onSelectLanguage() }

    utilPreferences.apply {

        if (appLanguage.isEmpty()) {
            appLanguage = LanguageHelper.getAppLanguage(this@ActivityIntro)
        }

        language?.let {
            binding.imgSelectLanguage.setImageDrawable(
                ContextCompat.getDrawable(
                    this@ActivityIntro,
                    LanguageHelper.getLanguageFlagRes(it)
                )
            )
        }

    }

}

private fun initializeAdapter() {

    introAdapter = IntroAdapter(this) {

        when (it) {

            0 -> {

                binding.lnrIndicatorOne.setBackgroundResource(R.drawable.onboard_item_selected_background)
                binding.lnrIndicatorTwo.setBackgroundResource(R.drawable.onboard_item_not_selected_background)
                binding.lnrIndicatorThree.setBackgroundResource(R.drawable.onboard_item_not_selected_background)

            }
            1 -> {

                binding.lnrIndicatorOne.setBackgroundResource(R.drawable.onboard_item_not_selected_background)
                binding.lnrIndicatorTwo.setBackgroundResource(R.drawable.onboard_item_selected_background)
                binding.lnrIndicatorThree.setBackgroundResource(R.drawable.onboard_item_not_selected_background)

            }
            2 -> {

                binding.lnrIndicatorOne.setBackgroundResource(R.drawable.onboard_item_not_selected_background)
                binding.lnrIndicatorTwo.setBackgroundResource(R.drawable.onboard_item_not_selected_background)
                binding.lnrIndicatorThree.setBackgroundResource(R.drawable.onboard_item_selected_background)

            }
        }

    }

    introAdapter.setVariables(binding.recyclerView)

    binding.recyclerView.adapter = introAdapter

    introAdapter.addItems(IntroInfo.getIntroItems())

}

private fun onSelectLanguage() {

    LanguageSelectDialog().showDialog(LanguageSelectionPageType.Intro) {

        binding.imgSelectLanguage.setImageDrawable(
            ContextCompat.getDrawable(
                this,
                it.flagRes
            )
        )

        utilPreferences.appLanguage = it.key

        startActivity(Intent(this, ActivitySplash::class.java))

        finish()

    }

}

} }

here is the full stack trace for Fatal Exception: java.lang.RuntimeException:这是致命异常的完整堆栈跟踪:java.lang.RuntimeException:

Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.makswin.fizbot/com.makswin.fizbot.ui.layout.modules.intro.activities.ActivityIntro}: android.view.InflateException: Binary XML file line #18 in com.makswin.fizbot:layout/layout_intro_slide_page: Binary XML file line #18 in com.makswin.fizbot:layout/layout_intro_slide_page: Error inflating class ImageView
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3654)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3806)
   at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
   at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
   at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2267)
   at android.os.Handler.dispatchMessage(Handler.java:107)
   at android.os.Looper.loop(Looper.java:237)
   at android.app.ActivityThread.main(ActivityThread.java:8167)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

and also for Caused by android.view.InflateException stack trace:以及 Caused by android.view.InflateException 堆栈跟踪:

Caused by android.view.InflateException: Binary XML file line #18 in com.****.****:layout/layout_intro_slide_page: Binary XML file line #18 in com.*****.****:layout/layout_intro_slide_page: Error inflating class ImageView

LanguageHelper:语言助手:

object LanguageHelper {
fun updateLanguage(context: Context) {
    val appLanguage = getAppLanguage(context)
    val res: Resources = context.resources
    val dm: DisplayMetrics = res.displayMetrics
    val conf = res.configuration
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1) {
        conf.locale = Locale(appLanguage)
        res.updateConfiguration(conf, dm)
    } else {
        conf.setLocale(Locale(appLanguage))
        context.createConfigurationContext(conf)
    }
}

fun getAppLanguage(context: Context): String {
    var appLanguage = UtilPreferences(context).appLanguage
    if (appLanguage.isEmpty()) {
        appLanguage =
            ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)
                .toString()
    }
    if (appLanguage.contains("-")) {
        appLanguage = appLanguage.split("-")[0]
    } else if (appLanguage.contains("_")) {
        appLanguage = appLanguage.split("_")[0]
    }
    return appLanguage
}

} }

open function getLanguageFlagRes:打开 function getLanguageFlagRes:

 fun getLanguageFlagRes(language: Language): Int {
        return when (language) {
            Language.Turkish -> {
                R.drawable.lang_tr_icon
            }
            Language.Portuguese -> {
                R.drawable.lang_pt_icon
            }
            Language.Romanian -> {
                R.drawable.lang_ro_icon
            }
            Language.Italian -> {
                R.drawable.lang_it_icon
            }
            Language.Spanish -> {
                R.drawable.lang_es_icon
            }
            else -> {
                R.drawable.lang_en_icon
            }
        }
    }

the question looks strange, may be you can do st to find what really cause the error这个问题看起来很奇怪,也许你可以找到真正导致错误的原因

1: delate the src to find if the picture cause this error 1:删除src以查找图片是否导致此错误

<ImageView
    android:id="@+id/imgSelectLanguage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    // android:src="@drawable/lang_en_icon"
    android:background="@color/transparent"
    android:scaleType="centerCrop" />

2: use AppCompatImageView replace ImageView 2:使用AppCompatImageView替换ImageView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM