Issue
imageSorry, I'm new to Java/Kotlin mobile apps...
Below code snippet from RegisterFragment.kt which is the main class:-
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val username = binding.text
PackageSdk.getInstance().hasDuplicateUserKey(**username**, object : PackageResponseCallback<ResultResponse> {
override fun onSuccess(result: ResultResponse) {
Log.i(TAG, "Result code : " + result.rtCode)
}
override fun onFailed(errorResult: ErrorResult) {
Log.e(TAG, "Error code : " + errorResult.errorCode)
}
})
whereas below is the data class named RegisteredUserView.kt
data class RegisteredUserView(
val username: String
//... other data fields that may be accessible to the UI
)
I usually used toString() to the param value of "username" but I will get this bug
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference.
but if I just leave only the username, I will have "Type mismatch. Required:String Found: EditText" type of error. Do I have to create a function to pass value from data class to RegisterFragment class in order to have proper param input. If yes then how? If no then what way to assign param input? Btw, the
val username = binding.text
is fetch from layout_fragmentregister (an EditText) which its id = text
Solution
The error you are getting is because
val username: String is null
your data class doesn't do much besides pass data along. You should use an instance of it and a view model to pass data to another fragment. A typical domain data class looks like this
data class User(
val userID: String,
val userName: String,
val emailAddress: String,
val photoUrl: String,
val firstName: String,
val lastName: String,
val age: Int,
val score: Int,
val sex: Int,
val emailVerified:Boolean)
to create an instance of the data class create a private var to store that data.
private var currentUser = User(someFunctionThatReturnsUserData)// the data must match the model
for test purposes and better understanding you can create a function with a test user
fun generateTestUser():User{return User("TEST ID","TEST NAME","TEST EMAIL")}
you can decide how to display the data in currentUser by accessing any of the properties. i prefer to do this in the view model as it removes a lot of logic from the UI thread and doesn't get reset on config changes.
currentUser.userId
currentUser.userName
currentUser.emailAddress
and so on. Usually best practice to create a private and public variables. Private to manipulate data, public to allow views to see the data but not modify it.
in your view model
private val _uid: MutableLiveData<String> by lazy { MutableLiveData<String>() }
val uid:LiveData<String> //this returns a string of live data for the ui to observe changes on.
get() = _uid //get the value from the private val
fun getUserId(){_uid.value = "some string"}
these are just examples and probably wont work with a simple copy/paste but hopefully helps a little.
Answered By - David
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.