I am obtaining the height data of a user through a query in Amplify's DataStore as follows:
val username = Amplify.Auth.currentUser.username
Log.i("Username actual",username)
var height = 0
var original = listOf(height)
Amplify.DataStore.query(
Users::class.java,
{ items ->
while (items.hasNext()) {
val item = items.next()
Log.i("Email", item.email)
Log.i("User", username)
if (item.email == username){
Log.i("Altura",item.height.toString())
height = item.height
original = original height
}
}
Log.i("HEight final", original.last().toString())
binding.tvheightUser.setText(original.last().toString())
},
{ failure -> Log.e("Tutorial", "Could not query DataStore", failure) }
)
As seen in the next line:
binding.tvheightUser.setText(original.last().toString())
I am trying to save in my text field the last value of the array of heights obtained. However, I can't display the data in my text field. The data is obtained correctly as verified in the following line:
Log.i("HEight final", original.last().toString())
I obtain the following error:
Could not query DataStore
DataStoreException{message=Error in querying the model., cause=android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views., recoverySuggestion=See attached exception for details.}
at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$4$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:382)
at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda3.run(Unknown Source:10)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8557)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1498)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at androidx.drawerlayout.widget.DrawerLayout.requestLayout(DrawerLayout.java:1353)
at android.view.View.requestLayout(View.java:25126)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3605)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at android.widget.ScrollView.requestLayout(ScrollView.java:1643)
at android.view.View.requestLayout(View.java:25126)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3605)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at android.view.View.requestLayout(View.java:25126)
at android.widget.TextView.checkForRelayout(TextView.java:9723)
at android.widget.TextView.setText(TextView.java:6301)
at android.widget.TextView.setText(TextView.java:6129)
at android.widget.TextView.setText(TextView.java:6081)
at ficade.app.ui.PerfilFragment.settingHeight$lambda-0(PerfilFragment.kt:60)
at ficade.app.ui.PerfilFragment.$r8$lambda$c-KDQKF9B3W3xW1gVqmOvgj3sMo(Unknown Source:0)
at ficade.app.ui.PerfilFragment$$ExternalSyntheticLambda1.accept(Unknown Source:10)
at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter.lambda$query$4$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:380)
at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda3.run(Unknown Source:10)
How could I solve it?
CodePudding user response:
The problem is that your callback to the DataStore query is not running on the main/UI thread, so you get an error from editing views off the UI thread. To fix that, you can wrap the view editing calls in either runOnUiThread
or use a coroutine on the main thread (using Dispatchers.Main
)
activity.runOnUiThread {
binding.tvheightUser.setText(original.last().toString())
}
or
lifecycleScope.launch(Dispatchers.Main) {
binding.tvheightUser.setText(original.last().toString())
}