Hello, everybody, I've a question, but it's a litt...
# android
h
Hello, everybody, I've a question, but it's a little bit difficult. I get an error " kotlin.UninitializedPropertyAccessException: lateinit property binding has not been initialized". binding cannot initialized in getRetrofitClient(). Is there any way or other technologies to fix this issue? Thanks in advance.
Copy code
class LoginFragment : Fragment() {

    private lateinit var binding: FragmentLoginBinding
    private val viewModel: AuthViewModel by viewModels()
    /* private val viewModel:AuthViewModel by lazy{
         ViewModelProvider(this).get(AuthViewModel::class.java)

     }*/



    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentLoginBinding.inflate(inflater, container, false)
     ........
   }

  fun reqBody(): String {

        Log.d("LogFrag", "reqBody's binding$binding")

        val clientIdentifier = binding.edittxtClientIdentifier.text.toString().trim()
       
        val pass = binding.editTextTextPassword.text.toString().trim()

        viewModel.login(
            clientIdentifier,
            pass
        )

        val rootObject = JSONObject()
        rootObject.put("ClientIdentifier", clientIdentifier)
        rootObject.put("Password", pass)
        encrypt(rootObject.toString(), publicKey)

        val encrypted = encrypt(rootObject.toString(), publicKey)

        return encrypted
    }
Copy code
fun <Request> getRetrofitClient(authenticator: Authenticator? = null): OkHttpClient {
 
    val finalEncr=reqBody()                //Error shows on this
      
    val body = "{\r\n    \"Data\":\"$finalEncr\"\r\n}"
        .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())


    return OkHttpClient.Builder()
        .addInterceptor { chain ->
            chain.proceed(chain.request().newBuilder().also {
                it.addHeader("Accept", "application/json")
                it.method("POST", body)

            }.build())
        }.also { client ->
            authenticator?.let { client.authenticator(it) }
            if (BuildConfig.DEBUG) {
                val logging = HttpLoggingInterceptor()
                logging.setLevel(HttpLoggingInterceptor.Level.BODY)
                client.addInterceptor(logging)
            }
        }.build()
}
🧵 4
i