每当我添加setOnClickListener
时,我的应用就无法在模拟器中运行。
package com.example.user.ag
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.connecting_methodes.*
import kotlinx.android.synthetic.main.login.*
class ConnectingActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_connecting)
showFragmentMethodes()
ag_login.setOnClickListener{showFragmentLogin()}//when commenting this the app work
//textView_st2.setOnClickListener{showFragmentRegister()}
}
fun showFragmentMethodes(){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_holder, ConnectingMethodes())
transaction.addToBackStack(null)
transaction.commit()
}
fun showFragmentLogin(){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_holder, Login())
transaction.addToBackStack(null)
transaction.commit()
}
fun showFragmentRegister(){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.fragment_holder, Register())
transaction.addToBackStack(null)
transaction.commit()
}
}
答案 0 :(得分:1)
您使用的视图不是来自您用作主要布局的R.layout.activity_connecting
:
import kotlinx.android.synthetic.main.connecting_methodes.*
import kotlinx.android.synthetic.main.login.*
我假设您仅通过调用showFragmentMethodes()
进行设置。但是此方法异步添加视图。因此ag_login
为null,您的应用会崩溃。
您可能要等到显示ConnectingMethodes
之后再连接按钮或使用片段本身来处理此连接。