Reputation: 3
I'm new to Android Kotlin and I can't access the value I inputted on xml for input_mtk ID, which always returns null on Kotlin file. Here's my code
XML file
<TextView
android:id="@+id/matematika"
android:layout_width="wrap_content"
android:layout_height="19dp"
android:layout_marginStart="56dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal|top"
android:text="@string/matematika"
android:textAppearance="@style/string_form"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/input_bahasa_inggris" />
<EditText
android:id="@+id/input_mtk"
style="@style/shape2"
android:layout_marginStart="56dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="230dp"
android:inputType="numberDecimal"
android:maxLength="5"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/matematika" />
Kotlin file
class ResultFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_result, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getData()
}
fun getData() {
var Mat = input_mtk.text.toString()
Log.e("test", Mat)
}
}
It shows this error
java.lang.NullPointerException: input_mtk must not be null
at com.xx.xx.ResultFragment.getData(ResultFragment.kt:38)
at com.xx.xx.ResultFragment.onViewCreated(ResultFragment.kt:33)
if I change
var Mat = input_mtk.text.toString()
to
var Mat = input_mtk?.text.toString()
shows clearly that input_mtk id returns null
2021-08-29 16:49:22.158 15887-15887/com.xx.xx E/test: null
Upvotes: 0
Views: 443
Reputation: 318
input_mtk.text
can be null within onViewCreated()
if the Kotlin synthetic imports or View bindings are not done correctly.
Also, you probably would not have entered anything in the EditText by the time onViewCreated()
has finished execution
Put it inside a button's click listener:
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And then in your Kotlin class, access it:
var input = ""
my_button.setOnClickListener {
input = input_mtk.text.toString()
}
Here input
will hold the string from your EditText
Note: I am assuming you are using Kotlin synthetics. If yes, it is recommended to migrate to View Bindings - https://developer.android.com/topic/libraries/view-binding/migration
Upvotes: 1
Reputation: 181
if you use viewBinding you can do like this
first use viewBinding
private lateinit var binding: FragmentResultBinding
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentResultBinding.inflate(inflater)
return binding.root
}
and change function getData like this
private fun getData(){
val input = binding.inputMtk.text.toString()
Log.e("FragmentResult", "getData: input: $input" )
}
Upvotes: 0
Reputation: 412
I do not really understand, what are you trying to do.
EditText
is a view where the user inputs some data. When you create your app and run it, it has a null
value aka "there is no content".
You can manually add text in your xml,if you want:
<EditText
android:id="@+id/input_mtk"
android:text="sometext"
... />
But its make zero sense since this view is designed to give user opportunity to input own data, and should be empty in most of the cases. Use TextView
instead
If you want to read inputed data, you can use button:
yourButton.setOnClickListener { input_mtk?.text.toString() }
Upvotes: 0