在主要活动之外获取EditText的文本
我刚开始使用Kotlin开发Android应用,并遇到以下问题: 我想为了娱乐而建立一个小登录。 我有点认为这是我试图通过v.findviewitembyid()
来获取文本的原因 但是,当执行应用程序时,我会带着错误java.lang.nullpoInterException:尝试调用虚拟方法'android.text.edext.edable android.widget.widget.edittext.getText()'null对象参考
my mainActivity.kt:imainActivity.kt :(删除imports)
class MainActivity : AppCompatActivity(){
private lateinit var B: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
B = ActivityMainBinding.inflate(layoutInflater)
setContentView(B.root)
val btnListener = ButtonClickListener()
B.btnLogin.setOnClickListener(btnListener)
}
}
和imports)和imports )和imports)和ButtonClickListener.kt:
package de.fynnhenck.login.listener
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import de.fynnhenck.login.MainActivity
import de.fynnhenck.login.R
import java.security.AccessController.getContext
class ButtonClickListener : View.OnClickListener {
override fun onClick(v: View?) {
val bu = v as Button
val loginText = v.findViewById<TextView>(R.id.tv_loginText)
val username = v.findViewById<EditText>(R.id.et_username)
val password = v.findViewById<EditText>(R.id.et_password)
if(username.text.equals("user") and password.text.equals("pass")){
loginText.text = "Test"
}
}
}
我希望这可以很好地解释我的问题。 谢谢您提前提供帮助:)
I just got into developing Android apps with Kotlin and run into the following Problem:
I wanted to build a small login just for fun.
I kinda figured that that is due to me trying to get the Text via v.findViewItemById()
but can't find another way/a way to go around it
But when executing the App crashes on me with the errorjava.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
My MainActivity.kt: (removed imports)
class MainActivity : AppCompatActivity(){
private lateinit var B: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
B = ActivityMainBinding.inflate(layoutInflater)
setContentView(B.root)
val btnListener = ButtonClickListener()
B.btnLogin.setOnClickListener(btnListener)
}
}
And the ButtonClickListener.kt:
package de.fynnhenck.login.listener
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import de.fynnhenck.login.MainActivity
import de.fynnhenck.login.R
import java.security.AccessController.getContext
class ButtonClickListener : View.OnClickListener {
override fun onClick(v: View?) {
val bu = v as Button
val loginText = v.findViewById<TextView>(R.id.tv_loginText)
val username = v.findViewById<EditText>(R.id.et_username)
val password = v.findViewById<EditText>(R.id.et_password)
if(username.text.equals("user") and password.text.equals("pass")){
loginText.text = "Test"
}
}
}
I hope this explains my issue well enough.
Thank ya'll for your help in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您调用
view> view> view> view> findviewbyid
(btnlogin
是您的情况)时,它会尝试在下降视图之间查找。因此,由于tv_logintext
不是您按钮的子视图,因此此行设置
logintext
tonull
。尝试通过绑定访问文本视图:
When you call
findViewById
of yourView
(btnLogin
is your case), it tries finding between descending views. So sincetv_loginText
is not child view of your button, this rowsets
loginText
tonull
.Try accessing your textviews via binding:
我相信它的崩溃是因为onclick(v:view?)仅包含您的按钮的视图,而不包含其他2个deDittext的视图,这就是您的零。
为什么两个文件又不直接在MainAttivity中添加此逻辑?
I believe its crashing because onClick(v: View?) only contains view of your button and not your 2 other editText and thats you are getting null.
Why two files and not directly adding this logic in MainActivity?