TextView为null [英] TextView is null

查看:132
本文介绍了TextView为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用TextView时遇到问题.按下按钮后,我想向该TextView附加内容,但是显然一直都是null.

I'm having a problem with TextView. After pressing a button I want to append something to that TextView but apparently is null all the time.

fragment_one.xml

fragment_one.xml

....
<TextView
    android:id="@+id/reply"
    android:layout_below="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/reply"/>
<Button android:text="@string/_0"
        android:id="@+id/_0"
        android:onClick="handleButtons"/>
....

ActivityTwo.java

ActivityTwo.java

....
public void handleButtons(View v){
    setContentView(R.layout.fragment_one);
    TextView tv = (TextView) v.findViewById(R.id.reply);
    if(v.getId() == R.id._0){
        tv.append("hi");
    }
....

我想在reply TextView上附加一些文本,但是很显然,它一直都返回NullPointerException.我迷路了,我不知道什么是失败的.

I want to append some text to the reply TextView but apparently, it returns NullPointerException all the time. I am lost, I don't know what is failing.

推荐答案

不使用v.findViewById,而是this.findViewById

Not use v.findViewById, but this.findViewById

这篇关于TextView为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆