[英]Failing at updating a TextView with Android Emulator
我正在尝试通过调用 setText() 方法来更新 TextView 对象的文本。 我直接向它提供了一个字符串值,但我无法让它在模拟器上运行的应用程序的 UI 上更新。
这发生在片段上(在 Android Studio 上创建具有简单活动的项目时自动生成的片段之一)
到目前为止,关于我的情况有几点:
我尝试使用 runOnUiThread“模式”调用 setText() 方法无济于事。
getActivity().runOnUiThread(new Runnable() { @Override public void run() { textView.setText("Service online"); } });
我检查了 TextView 实例的属性 mText。 它已更新。 它只是不会在 UI 上更新:/
简而言之,无论我尝试做什么,UI 元素都会坚持在 XML 片段文件上设置的任何字符串值(或者没有值,如果我删除 android:text 属性)。 Stack Overflow 上与此问题类似的其他帖子也对我没有帮助。 知道它可能是什么吗?
另外,我发布了与 java 代码相关的整个片段:
public class FirstFragment extends Fragment
{
public Gson serializer;
public TextView textView;
private NetworkManager networkManager;
private boolean serviceIsBound;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
textView = (TextView) view.findViewById(R.id.main_window);
textView.setText(R.string.app_name);
return inflater.inflate(R.layout.fragment_first, container, false);
}
@Override
public void onStart() {
super.onStart();
Intent bindIntent = new Intent(getActivity(), NetworkManager.class);
getActivity().bindService(bindIntent, serviceConnection, Context.BIND_AUTO_CREATE);
}
@Override
public void onStop()
{
super.onStop();
getActivity().unbindService(serviceConnection);
serviceIsBound = false;
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v("DEV UPDATE", "Starting Request ");
if (serviceIsBound)
{
GetAPIStatusResult result = networkManager.GetAPIStatus();
if (result.GetStatus())
{
Log.v("REQUEST RESULT", "API is Fine");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("Service online");
}
});
}
else
{
Log.v("REQUEST RESULT", "API is Down or a problem occurred");
textView.setText("Service down");
}
}
}
});
}
private ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName className, IBinder service)
{
NetworkManager.NetworkManagerServiceBinder binder = (NetworkManager.NetworkManagerServiceBinder) service;
networkManager = binder.GetService();
serviceIsBound = true;
}
@Override
public void onServiceDisconnected(ComponentName arg)
{
serviceIsBound = false;
}
};
}
片段的关联 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<TextView
android:id="@+id/main_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Here will appear API status"
app:layout_constraintBottom_toTopOf="@id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="SendRequest"
android:text="@string/SendRequest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/main_window" />
</androidx.constraintlayout.widget.ConstraintLayout>
正如用户Cheticamp评论的那样,我的 onCreateView() 方法出现了问题,我调用了 infalter.inflate() 方法两次,并且没有返回我的视图 object。
我用我的视图 object 的返回替换了第二个 inflate() 方法调用,它立即起作用了! 我的 UI 现在正在按预期更新!
您正在尝试引用属于该活动的视图。 如果要更新活动中的某些内容,则需要查看其他方法,而不是尝试直接引用视图。
一个好的起点是您传递给活动创建的片段的接口。 当你想设置下一个时,从片段中调用接口方法。 然后让活动处理更新。 这也更干净,因为每个视图只对自己的元素负责。
您也不需要使用runOnUIThread
因为onViewCreated
不是 ansychronos function 无论如何您已经在 UI 线程上。
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.