创建设置活动时发生布尔参数问题

时间:2018-12-05 15:35:05

标签: android android-activity kotlin boolean

为“设置”页面创建活动后,我注意到mCurrentValue !== value中的if (mCurrentValue !== value)返回了警告:

  

布尔型参数的身份相等性?和布尔值可能会由于隐式拳击而变得不稳定

我尝试通过在?中的Boolean旁边添加override fun onXchange(value:Boolean) {来解决此警告,但随后返回以下错误:

  

'onXchange'不会覆盖任何内容

关于如何解决此问题的任何想法?

活动课程

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.preference.PreferenceManager
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem

class MySettingsActivity : AppCompatActivity(), MySettingsFragment.PreferenceXchangeListener {
    private var mCurrentValue: Boolean? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        mCurrentValue = mSharedPreferences.getBoolean("preference_a", false)
        if (mCurrentValue as Boolean)
        {
            setTheme(R.style.MyDarkAppCompatTheme)
        }
        else
        {
            setTheme(R.style.MyLightAppCompatTheme)
        }
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mysettings)

        val settingsFragment = MySettingsFragment()
        supportFragmentManager
                .beginTransaction()
                .replace(R.id.settings_container, settingsFragment)
                .commit()

        val myActionBar = actionBar

        if (myActionBar != null)
        {
            myActionBar.setTitle(R.string.settings)
            myActionBar.setBackgroundDrawable(ColorDrawable(Color.BLACK))
            myActionBar.setDisplayHomeAsUpEnabled(true)
            myActionBar.setDisplayShowHomeEnabled(true)
            myActionBar.setHomeAsUpIndicator(resources.getDrawable(R.drawable.ic_arrow_back_white, null))
        }
    }

    override fun onXchange(value:Boolean?) {
        if (mCurrentValue !== value) {
            mCurrentValue = value
            recreate()
        }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                val intent = parentActivityIntent
                intent?.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
                onBackPressed()
                return true
            }

            else ->
                return super.onOptionsItemSelected(item)
        }
    }
}

片段类

class MySettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.app_preferences)

        val mCheckBoxPreference = findPreference("preference_a") as CheckBoxPreference
        mCheckBoxPreference.onPreferenceChangeListener = this
    }

    private var mPreferenceXchangeListener: PreferenceXchangeListener? = null

    interface PreferenceXchangeListener {
        fun onXchange(value:Boolean)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)

        try
        {
            mPreferenceXchangeListener = context as MySettingsFragment.PreferenceXchangeListener
        }
        catch (e:ClassCastException) {
            Log.e(TAG, "onAttach::::: PreferenceXchangeListener must be set in parent Activity")
        }
    }

    override fun onPreferenceChange(preference: Preference, newValue:Any):Boolean {
        val preferenceKey = preference.key

        if (preferenceKey == "preference_a")
        {
            (preference as CheckBoxPreference).isChecked = newValue as Boolean

            mPreferenceXchangeListener!!.onXchange(newValue)
            return true
        }

        return false
    }

    companion object {
        private val TAG = MySettingsFragment::class.java.simpleName
    }
}

1 个答案:

答案 0 :(得分:2)

所以首先我可以解释它描述的问题。让我们提出一个非常人为的方案:

BooleanProvider.java

class BooleanProvider {
    @NonNull Boolean wrapMyBoolean(boolean state) {
        return new Boolean(state);
    }
}

BooleanProviderTest.kt

class BooleanProviderTest {
    @Test fun `it returns a true value when true is provided`() {
        assertSame(BooleanProvider().wrapMyBoolean(true), true)
    }
}

该测试实际上将失败,因为Boolean的实例不相同。在Java代码中,我们初始化了Boolean的新实例(而不是将原语自动装箱到{{时会得到的静态定义的Boolean.TRUEBoolean.FALSE实例)。 1}})。因此,为避免潜在的意外结果,建议您不要通过引用比较这些类型。

最简单的解决方法是将相等性测试更改为java.lang.Boolean而不是!=。这将执行身份相等性测试,而不是引用相等性测试,并且将在任一端适当地处理null值。无论如何,这就是您想要的99%的时间。

第二,如果不需要以特定方式处理null值,则也可以只将!==声明为非null类型,并为其提供默认值。只需将其声明为:

mCurrentValue