Kotlin:在 Amplify DataStore 中设置带有查询结果的文本
Kotlin: Set text with query result in Amplify DataStore
我在Amplify的DataStore中通过查询获取用户的身高数据如下:
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) }
)
如下一行所示:
binding.tvheightUser.setText(original.last().toString())
我试图在我的文本字段中保存获得的高度数组的最后一个值。但是,我无法在我的文本字段中显示数据。
数据已正确获取,如下行验证:
Log.i("HEight final", original.last().toString())
我收到以下错误:
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$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$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:380)
at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda3.run(Unknown Source:10)
我该如何解决?
问题是您对 DataStore 查询的回调不是 main/UI 线程上的 运行,因此您在 UI 线程之外编辑视图时会出错。要解决此问题,您可以将视图编辑调用包装在 runOnUiThread
中或在主线程上使用协程(使用 Dispatchers.Main
)
activity.runOnUiThread {
binding.tvheightUser.setText(original.last().toString())
}
或
lifecycleScope.launch(Dispatchers.Main) {
binding.tvheightUser.setText(original.last().toString())
}
我在Amplify的DataStore中通过查询获取用户的身高数据如下:
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) }
)
如下一行所示:
binding.tvheightUser.setText(original.last().toString())
我试图在我的文本字段中保存获得的高度数组的最后一个值。但是,我无法在我的文本字段中显示数据。 数据已正确获取,如下行验证:
Log.i("HEight final", original.last().toString())
我收到以下错误:
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$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$com-amplifyframework-datastore-storage-sqlite-SQLiteStorageAdapter(SQLiteStorageAdapter.java:380)
at com.amplifyframework.datastore.storage.sqlite.SQLiteStorageAdapter$$ExternalSyntheticLambda3.run(Unknown Source:10)
我该如何解决?
问题是您对 DataStore 查询的回调不是 main/UI 线程上的 运行,因此您在 UI 线程之外编辑视图时会出错。要解决此问题,您可以将视图编辑调用包装在 runOnUiThread
中或在主线程上使用协程(使用 Dispatchers.Main
)
activity.runOnUiThread {
binding.tvheightUser.setText(original.last().toString())
}
或
lifecycleScope.launch(Dispatchers.Main) {
binding.tvheightUser.setText(original.last().toString())
}