变化的TextView文本 [英] Change TextView text

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

问题描述

我试图改变从code我的的TextView 文本。

I'm trying to change my TextView text from the code.

这是我的XML是这样的:

This is what my xml looks like:

XML:
<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal" />

而code:

And the code:

TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
setContentView(tv1);

我在我的设备上得到一个错误,应用程序停止。 我试图表明一个的TextView (未连接到一个XML 的TextView )和它的工作。

I'm getting an error on my device and the application stops. I tried to show a TextView (not connected to an XML TextView) and it worked.

推荐答案

您的做法是不正确。我认为这将是空指针异常。更好地发布日志猫

Your approach is incorrect. I think it will be Null Pointer Exception. Better to post log cat

正确的位置

 setContentView(R.layout.yourlayout):

在此行应指定要使用的布局。

In this line you should specify the layout you are using.

然后

TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");

研究这里它到底是什么,你想要要知道

Study here It is what exactly you want to know

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

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