com.example.myapplication.MainActivity 无法投射 Android Studio kotlin
com.example.myapplication.MainActivity cannot be cast Android Studio kotlin
我创建了一个 Login/Register
页面,但在尝试单击 Register Button
时出现此错误。此方法应将我发送到注册页面,我可以在那里注册一个新的 account.The 问题是单击注册按钮时出现此错误:android app code error
我该如何解决这个错误?
这是我的代码和其他截图:
class LoginFragment : Fragment() {
private lateinit var username: EditText
private lateinit var password: EditText
private lateinit var fAuth: FirebaseAuth
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_login, container, false)
username = view.findViewById(R.id.log_username)
password = view.findViewById(R.id.log_password)
fAuth = Firebase.auth
view.findViewById<Button>(R.id.btn_register).setOnClickListener {
val navRegister = activity as FragmentNav
navRegister.navigateFrag(RegisterFragment(),false)
}
view.findViewById<Button>(R.id.btn_login).setOnClickListener {
validateForm()
}
return view
}
private fun firebaseSignIn(){
fAuth.signInWithEmailAndPassword(username.text.toString(),password.text.toString()).addOnCompleteListener{
task ->
if(task.isSuccessful){
}else{
Toast.makeText(context,"Register Successful",Toast.LENGTH_SHORT).show()
var navHome = activity as FragmentNav
navHome.navigateFrag(HomeFragment(),addToStack = true)
}
}
}
private fun validateForm() {
val icon = AppCompatResources.getDrawable(requireContext(), R.drawable.erroricon)
icon?.setBounds(0, 0, icon.intrinsicWidth, icon.intrinsicHeight)
when {
TextUtils.isEmpty(username.text.toString().trim()) -> {
username.setError("Please Enter Username")
}
TextUtils.isEmpty(password.text.toString().trim()) -> {
password.setError("Please Enter Password")
}
username.text.toString().isNotEmpty() &&
password.text.toString().isNotEmpty() -> {
if (username.text.toString().matches(Regex("[a-zA-Z0-9._]+@[a-z]+\\.+[a-z]+"))) {
firebaseSignIn()
} else {
username.setError("Please Enter Valid Email Id")
}
}
}
}
我在这里收到这个错误:
error
FragmentNav {
fun navigateFrag(fragment: Fragment, addToStack:Boolean )
}
这是按钮之间的导航 register/login :
class LoginActivity : AppCompatActivity(R.layout.login_activity),FragmentNav {
private lateinit var fAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login_activity)
fAuth = Firebase.auth
val currentUser = fAuth.currentUser
if(currentUser !=null){
supportFragmentManager.beginTransaction()
.add(com.google.android.material.R.id.container,HomeFragment()).addToBackStack(null)
.commit()
}
supportFragmentManager.beginTransaction()
.add(com.google.android.material.R.id.container,LoginFragment())
.commit()
}
override fun navigateFrag(fragment: Fragment, addToStack: Boolean) {
val transaction = supportFragmentManager
.beginTransaction()
.replace(com.google.android.material.R.id.container,fragment)
if(addToStack){
transaction.addToBackStack(null)
}
transaction.commit()
}
}
如果您不将 MainActivity
上下文传递给 LoginFragment
,该接口将完全无用。将 LoginFragment
构造函数更改为:
class LoginFragment(private val mListener: FragmentNav) : Fragment()
然后在注册按钮的onClick()
中,像这样调用navigateFrag
:
mListener.navigateFrag(RegisterFragment(),false)
最后在 MainActivity
中,像这样将上下文传递给 LoginFragment
:
supportFragmentManager.beginTransaction()
.add(com.google.android.material.R.id.container,LoginFragment(this))
.commit()
我创建了一个 Login/Register
页面,但在尝试单击 Register Button
时出现此错误。此方法应将我发送到注册页面,我可以在那里注册一个新的 account.The 问题是单击注册按钮时出现此错误:android app code error
我该如何解决这个错误?
这是我的代码和其他截图:
class LoginFragment : Fragment() {
private lateinit var username: EditText
private lateinit var password: EditText
private lateinit var fAuth: FirebaseAuth
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_login, container, false)
username = view.findViewById(R.id.log_username)
password = view.findViewById(R.id.log_password)
fAuth = Firebase.auth
view.findViewById<Button>(R.id.btn_register).setOnClickListener {
val navRegister = activity as FragmentNav
navRegister.navigateFrag(RegisterFragment(),false)
}
view.findViewById<Button>(R.id.btn_login).setOnClickListener {
validateForm()
}
return view
}
private fun firebaseSignIn(){
fAuth.signInWithEmailAndPassword(username.text.toString(),password.text.toString()).addOnCompleteListener{
task ->
if(task.isSuccessful){
}else{
Toast.makeText(context,"Register Successful",Toast.LENGTH_SHORT).show()
var navHome = activity as FragmentNav
navHome.navigateFrag(HomeFragment(),addToStack = true)
}
}
}
private fun validateForm() {
val icon = AppCompatResources.getDrawable(requireContext(), R.drawable.erroricon)
icon?.setBounds(0, 0, icon.intrinsicWidth, icon.intrinsicHeight)
when {
TextUtils.isEmpty(username.text.toString().trim()) -> {
username.setError("Please Enter Username")
}
TextUtils.isEmpty(password.text.toString().trim()) -> {
password.setError("Please Enter Password")
}
username.text.toString().isNotEmpty() &&
password.text.toString().isNotEmpty() -> {
if (username.text.toString().matches(Regex("[a-zA-Z0-9._]+@[a-z]+\\.+[a-z]+"))) {
firebaseSignIn()
} else {
username.setError("Please Enter Valid Email Id")
}
}
}
}
我在这里收到这个错误: error
FragmentNav {
fun navigateFrag(fragment: Fragment, addToStack:Boolean )
}
这是按钮之间的导航 register/login :
class LoginActivity : AppCompatActivity(R.layout.login_activity),FragmentNav {
private lateinit var fAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login_activity)
fAuth = Firebase.auth
val currentUser = fAuth.currentUser
if(currentUser !=null){
supportFragmentManager.beginTransaction()
.add(com.google.android.material.R.id.container,HomeFragment()).addToBackStack(null)
.commit()
}
supportFragmentManager.beginTransaction()
.add(com.google.android.material.R.id.container,LoginFragment())
.commit()
}
override fun navigateFrag(fragment: Fragment, addToStack: Boolean) {
val transaction = supportFragmentManager
.beginTransaction()
.replace(com.google.android.material.R.id.container,fragment)
if(addToStack){
transaction.addToBackStack(null)
}
transaction.commit()
}
}
如果您不将 MainActivity
上下文传递给 LoginFragment
,该接口将完全无用。将 LoginFragment
构造函数更改为:
class LoginFragment(private val mListener: FragmentNav) : Fragment()
然后在注册按钮的onClick()
中,像这样调用navigateFrag
:
mListener.navigateFrag(RegisterFragment(),false)
最后在 MainActivity
中,像这样将上下文传递给 LoginFragment
:
supportFragmentManager.beginTransaction()
.add(com.google.android.material.R.id.container,LoginFragment(this))
.commit()