菜单选择导致的Android InflateException

时间:2018-02-12 20:03:42

标签: android android-layout android-fragments kotlin

我正在使用偏好片段,我的XML上有一个例外。它曾经工作,但我已经调整了一些东西,现在当我按下菜单右上方的3个点时它会一直崩溃。

实际错误是:

  

java.lang.RuntimeException:无法启动活动ComponentInfo {org.sherman.android.babynme / org.sherman.android.babynme.Activites.SettingsActivity}:android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误膨胀类片段

这是我的activity_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android='http://schemas.android.com/apk/res/android'
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_settings"

    android:name="org.sherman.android.babynme.Fragments.SettingsFragment" />

my preferencesXML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <EditTextPreference
        android:defaultValue="Name"
        android:key="baby_name"
        android:title="Baby's Name" />
    <EditTextPreference
        android:defaultValue="01/01/01"
        android:key="baby_dob"
        android:title="Baby's Birthday" />
    <EditTextPreference
        android:numeric="integer"
        android:defaultValue="21"
        android:key="mum_age"
        android:singleLine="true"
        android:title="Your age" />
</PreferenceScreen>

我的设置活动.kt:

class SettingsActivity : AppCompatActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)

        val sharedPreferences: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        val babyName:String = sharedPreferences.getString("baby_name", "Baby")
        title = babyName+" 'n Me"



        val actionBar = this.supportActionBar
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        val id = item?.itemId
        if(id == android.R.id.home){
            NavUtils.navigateUpFromSameTask(this)
        }
        return super.onOptionsItemSelected(item)
    }
}

以及SettingsFragment的代码:

class SettingsFragment: PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
    val mContext = context

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.pref_baby)
        val sharedPreferences: SharedPreferences = android.preference.PreferenceManager.getDefaultSharedPreferences(activity.applicationContext)
        onSharedPreferenceChanged(sharedPreferences, "baby_name")
        onSharedPreferenceChanged(sharedPreferences, "baby_dob")
        onSharedPreferenceChanged(sharedPreferences, "mum_age")

    }

    override fun onResume() {
        super.onResume()
        preferenceScreen.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
        //activity.getSharedPreferences(context.packageName, Context.MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this)
    }

    override fun onPause() {
        super.onPause()
        preferenceScreen.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
        //activity.getSharedPreferences(context.packageName,Context.MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this)
    }

    override fun onSharedPreferenceChanged(p0: SharedPreferences?, key: String?) {
        Log.d(DEBUG, "Ospc called")

        val pref: Preference = findPreference(key)
        val etp: EditTextPreference = pref as EditTextPreference
        pref.setSummary(etp.text)

    }

以下是完整的错误日志:

                                                                           --------- beginning of crash
02-12 19:44:06.931 4518-4518/org.sherman.android.babynme E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: org.sherman.android.babynme, PID: 4518
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{org.sherman.android.babynme/org.sherman.android.babynme.Activites.SettingsActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                               at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                               at android.os.Looper.loop(Looper.java:164)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                            Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class fragment
                                                                            Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class fragment
                                                                            Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
                                                                               at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:255)
                                                                               at android.support.v7.preference.Preference.getPersistedString(Preference.java:1601)
                                                                               at android.support.v7.preference.EditTextPreference.onSetInitialValue(EditTextPreference.java:92)
                                                                               at android.support.v7.preference.Preference.dispatchSetInitialValue(Preference.java:1517)
                                                                               at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1245)
                                                                               at android.support.v7.preference.Preference.onAttachedToHierarchy(Preference.java:1257)
                                                                               at android.support.v7.preference.PreferenceGroup.addPreference(PreferenceGroup.java:194)
                                                                               at android.support.v7.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:126)
                                                                               at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:355)
                                                                               at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:162)
                                                                               at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:112)
                                                                               at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:138)
                                                                               at android.support.v7.preference.PreferenceFragmentCompat.addPreferencesFromResource(PreferenceFragmentCompat.java:432)
                                                                               at org.sherman.android.babynme.Fragments.SettingsFragment.onCreatePreferences(SettingsFragment.kt:20)
                                                                               at android.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:224)
                                                                               at android.support.v4.app.Fragment.performCreate(Fragment.java:2339)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1377)
                                                                               at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1640)
                                                                               at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1896)
                                                                               at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3673)
                                                                               at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
                                                                               at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338)
                                                                               at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:39)
                                                                               at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
                                                                               at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                               at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                                               at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
02-12 19:44:06.933 4518-4518/org.sherman.android.babynme E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                               at org.sherman.android.babynme.Activites.SettingsActivity.onCreate(SettingsActivity.kt:15)
                                                                               at android.app.Activity.performCreate(Activity.java:6975)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                               at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                               at android.os.Looper.loop(Looper.java:164)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我的问题是我改变了什么导致菜单选择现在崩溃? (谢谢你)。

1 个答案:

答案 0 :(得分:2)

问题是Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

您必须在某处更改过数据类型。