编写代码的时候遇到这个报错“Wrapped into a reference object to be modified when captured in a closure”
没百度出结果,中文就是说“包裹到要在关闭中捕获时要修改的参考对象中”
下面的代码中,所有的fragflag常量都有警告,想请教各位讲一下原理。
问题代码如下:
class MainActivityRSC : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_rsc)
supportActionBar?.hide()
var fragflag = 0 //这里fragflag警告:Typo: In word 'fragflag'
val buttonRSC0: Button = findViewById(R.id.RSC_General)
val buttonRSC1: Button = findViewById(R.id.RSC_1)
val buttonRSC11: Button = findViewById(R.id.RSC_MenuBack)
buttonRSC0.setOnClickListener {
if(fragflag != 0) { //这里fragflag警告:Wrapped into a reference object to be modified when captured in a closure
replaceFragment(RightFragRSC0())
fragflag = 0 //这里fragflag警告:Wrapped into a reference object to be modified when captured in a closure
}
}
buttonRSC1.setOnClickListener {
if(fragflag != 1) { //这里fragflag警告:Wrapped into a reference object to be modified when captured in a closure
replaceFragment(RightFragRSC1())
fragflag = 1 //这里fragflag警告:Wrapped into a reference object to be modified when captured in a closure
}
}
buttonRSC11.setOnClickListener {
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)
}
replaceFragment(RightFragRSC0())
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val transaction = fragmentManager.beginTransaction()
transaction.replace(R.id.rightLayout, fragment)
transaction.commit()
}
}