"App keep stopping" 在模拟器中添加 setOnClickListener 后
"App keep stopping" in the emulator after adding setOnClickListener
每当我添加 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()
}
}
您使用的视图并非来自您用作主要布局的 R.layout.activity_connecting
:
import kotlinx.android.synthetic.main.connecting_methodes.*
import kotlinx.android.synthetic.main.login.*
我假设您只通过调用 showFragmentMethodes()
来设置它。但是这种方法异步添加视图。因此 ag_login
为空,您的应用程序崩溃。
您可能希望等到显示 ConnectingMethodes
后再连接按钮或使用片段本身处理此连接。
每当我添加 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()
}
}
您使用的视图并非来自您用作主要布局的 R.layout.activity_connecting
:
import kotlinx.android.synthetic.main.connecting_methodes.*
import kotlinx.android.synthetic.main.login.*
我假设您只通过调用 showFragmentMethodes()
来设置它。但是这种方法异步添加视图。因此 ag_login
为空,您的应用程序崩溃。
您可能希望等到显示 ConnectingMethodes
后再连接按钮或使用片段本身处理此连接。