gpt4 book ai didi

android - 创 build 置 Activity 时出现 boolean 参数问题

转载 作者:行者123 更新时间:2023-11-29 18:38:00 30 4
gpt4 key购买 nike

在为设置页面创建 Activity 后,我注意到 if (mCurrentValue !== value) 中的 mCurrentValue !== value 返回警告:

Identity equality for arguments of types Boolean? and Boolean can be unstable because of implicit boxing

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

'onXchange' overrides nothing

关于如何解决这个问题有什么想法吗?

Activity 类

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)
}
}
}

fragment 类

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
}
}

最佳答案

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

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 实例)当一个原语被自动装箱到一个 java.lang.Boolean 时,你会得到)。因此,为避免潜在的意外结果,建议您不要通过引用来比较这些类型。

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

其次,如果您不需要以特定方式处理空值,您也可以只将mCurrentValue 声明为非空类型,并为其指定一个默认值。只需将其声明为:

private var mCurrentValue: Boolean = false // false is the default here

关于android - 创 build 置 Activity 时出现 boolean 参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53635745/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com