【问题标题】:Failing at updating a TextView with Android Emulator使用 Android 模拟器更新 TextView 失败
【发布时间】: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


【解决方案1】:

正如用户 Cheticamp 评论的那样,我的 onCreateView() 方法存在问题,我在该方法中调用了 infalter.inflate() 方法两次,但没有返回我的视图对象。

我用我的视图对象的返回替换了第二个 inflate() 方法调用,它立即起作用了!我的 UI 现在正在按预期更新!

【讨论】:

    【解决方案2】:

    您正在尝试引用属于该活动的视图。如果您想更新 Activity 中的某些内容,您需要查看其他方法,而不是尝试直接引用视图。

    一个好的起点是您传递给由活动创建的片段的接口。当你想设置下一个时,从片段中调用接口方法。然后让活动处理更新。这也更简洁,因为每个视图只负责自己的元素。

    您也不需要使用 runOnUIThread,因为 onViewCreated 不是 ansychronos 函数,您已经在 UI 线程上。

    希望对您有所帮助。

    【讨论】:

    • 当你提到我的代码“试图引用属于活动的视图”时,你的意思是 TextView?因为我会说 TextView 属于片段(如,它的 xml 声明在 fragment_first.xml 中)。也会发在这里
    猜你喜欢
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 2011-07-08
    • 1970-01-01
    • 2017-10-28
    • 2011-09-08
    相关资源
    最近更新 更多