0

Android studio Bumblebee 2021.1.1

All I am simply trying to do is get or retrieve text input from edittext field and it simply doesn't work no matter how I do it. I have searched everywhere including stackoverflow for solution or answer but with no success. Here is my code.

...
val courseNameEdt = findViewById<EditText>(R.id.idEdtCourseName)
...
addCourseBtn.setOnClickListener{
        
        var courseName = courseNameEdt.text.gettext() //doesn't work | unresolved reference gettext() and won't compile
        //var courseName = courseNameEdt.text           doesn't work either | app compiles, crashes and stops running when launched
        //var courseName = courseNameEdt.text.toString()doesn't work either | app compiles, crashes and stops running when launched
}

So, what am I doing wrong? Is there a fourth ways to do this?

enter image description here

3
  • 2
    "crashes and stops running when launched" -- use Logcat to examine the stack trace associated with the crash. If you do not understand the stack trace, use the "Edit" link to add it to the question. FWIW, note that Bumblebee has been replaced several times in the past three years. Commented Nov 27, 2024 at 20:14
  • That app can also crash the app as we can see that the null values are not handled in the code given. Can you share the Error which came at the time of app crashing? Commented Nov 28, 2024 at 6:57
  • 1
    @Vishal Shah The code most probably doesn't need any null handling - it's totally fine to crash on such an obvious bug as "accessing a non existing label" Commented Nov 28, 2024 at 7:11

2 Answers 2

1

The First method var courseName = courseNameEdt.text.gettext() won't work obviously.

The second and third ones are correct syntactically but the crashed might be for different reasons, which would be helpful if you could share.

Now there can be any problem like improper id, or if you are you using in onViewCreated() of fragment then you need an instance of the view from onCreateView() while inflating the layout.

So in short the correct way to get text from an EditText is this:

 var courseName = courseNameEdt.text.toString()

there is another way to get and set text with binding, but i guess you are a beginner and hence wouldn't have any idea about that.

So, the crashes might be due to different reasons but the correct way to fetch text is the ways mentioned above.

Sign up to request clarification or add additional context in comments.

Comments

0

"courseNameEdt.text" is the answer. The reason that it crashes is because the view with id "idEdtCourseName" is either inaccessible on that screen or is not an EditText. Use ViewBinding.

1 Comment

Alternatively, use compose. Especially when just starting to learn Android and not having to maintain an old codebase :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.