【发布时间】:2021-07-06 20:10:51
【问题描述】:
我正在尝试通过调用 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>
【问题讨论】:
-
你的代码看起来不错除了你在
onCreateView()中膨胀了两次布局。第一次充气并返回view而不是第二次充气。 -
谢谢。进行更改...
-
是的!对对对!谢谢 :D 这么久终于解决了这个问题!非常感谢!
-
想给代表留下答案吗? @Cheticamp
标签: java android user-interface textview