I just started with that new App and wanted to see if I get any Response by Retrofit and print it in a TextView.
But the App crashes without any Stack Trace so no Exception ... just nothing.
I have a Retrofit interface and a "Factory" that creates the Request everything runs in a different Thread via Kotlin Coroutine.
class MainActivity : AppCompatActivity() {
private lateinit var debugTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
debugTextView = findViewById(R.id.debugTextView)
val service = RetrofitFactory.makeCarDataService()
GlobalScope.launch(Dispatchers.Main) {
val request = service.getData()
val response = request.await()
debugTextView.text =response.toString()
}
}
}
I would love to post an Error ... but there is none, everything should run perfectly :/
To use Dispatchers.Main we need to add the following line to the app's build.gradle file dependencies:
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
Absence of that dependency could be the reason why the app crashes without any stack trace.
Without stacktrace these are the possible problems:
service.getData()
can throw NullPointerException
debugTextView.text
can throw NullPointerException
response.toString()
can throw NullPointerException
I'm not very familiar with the coroutines, but have you tried to use the main thread only while setting the text on the view? something like this:
val service = RetrofitFactory.makeCarDataService()
GlobalScope.launch(Dispatchers.Default/*change here*/) {
val request = service.getData()
val response = request.await()
/*change here*/
withContext(Dispatchers.Main) {
debugTextView.text = response.toString()
}
}
}