android TextView setText 不工作

标签 android textview

我知道有很多类似的线程,但我已经遍历了它们,但仍然无法找出问题所在。我的程序到达处理程序,但它总是返回捕获异常“消息未处理。”

我声明了 TextView 私有(private) TextView 聊天框;

在 onCreate 下我有:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpViews();
        setUpListener();
    }

setUpViews() fragment 看起来像:

 private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    }

处理程序:

public Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg){
            try{
                chatbox.setText("Got it!");
            }catch(Exception e){
                Log.i("MYLOG", "Message was not handled.");
            }

        }
    };

main.xml 文件中的 fragment :

<TextView
        android:id="@+id/chatbox"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

最佳答案

为了将来引用,我有多个 fragment ,每个 fragment 对应我的 ViewPager 中的每个选项卡。一个 TextView 在两个 fragment 中具有相同的 id,这造成了冲突。我没有意识到我有相同的 id 所以一切看起来都很好,没有错误消息,但文本根本没有改变。因此,我建议为 id 使用唯一的名称。

关于android TextView setText 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18347016/

相关文章:

android - Android中如何从TimePicker获取值时间

android - 如何将评级栏设置为最低一颗星?

android - 自定义文本跨度不起作用

android - 我可以改变 clickablespan 的外观和感觉吗

ListView中的Android圆角

java - android - 在 try 语句中忽略返回

android - 一次突出显示两个 TextView

android - 设置字母间距的字符样式/跨度

违反 Android Branch IO SDK 政策

android - 在 textview 中获取当前可见文本