类中的 TextView 和 setText
我知道有人对此有一些疑问,但我仍然不明白。 我有一个活动
package test.example.om;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import test.example.om.Texter;
public class TextActivity extends Activity {
/** Called when the activity is first created. */
public String text="Helloo";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Texter myTexter = new Texter();
myTexter.textTexter();
}
public void textSet(){
TextView tv = (TextView) findViewById(R.id.myTextViewInXml);
tv.setText(text);
}
}
和一个类 Texter
package test.example.om;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Texter extends Activity{
String string="Helloo";
public void textTexter(){
TextView tv = (TextView) findViewById(R.id.myTextViewInXml);
tv.setText(string);
}
}
LogCat 显示 NullPointerException 并且应用程序崩溃。我做错了什么以及如何从主活动类之外的另一个类将Text设置为TextView?
I know there's a couple of questions about this out there but I still don't get it.
I have an activity
package test.example.om;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import test.example.om.Texter;
public class TextActivity extends Activity {
/** Called when the activity is first created. */
public String text="Helloo";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Texter myTexter = new Texter();
myTexter.textTexter();
}
public void textSet(){
TextView tv = (TextView) findViewById(R.id.myTextViewInXml);
tv.setText(text);
}
}
And a class Texter
package test.example.om;
import android.widget.TextView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Texter extends Activity{
String string="Helloo";
public void textTexter(){
TextView tv = (TextView) findViewById(R.id.myTextViewInXml);
tv.setText(string);
}
}
The LogCat is showing a NullPointerException and the app crashes. What am I doing wrong and how can I setText to the TextView from another class than the main activity class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在实例化一个活动子类,但您不应该这样做......
另外,您尝试在上下文为空时查找 ViewById (在 Texter 类中),因为您尚未经历正确的 Activity 生命周期。
为了更改文本视图的文本,只需将 textTexter 方法从 Texter 移动到 TextActivity 并调用它即可。
删除未使用的 Texter 活动。
编辑:刚刚注意到你想从另一个班级做这件事。为此,只需提供对要更改的文本视图的引用并对其进行 setText
You are instantiating an activity subclass and you're not supposed to do that...
Also, you are trying to findViewById (in the Texter class) while your context is null, since you haven't gone through the correct Activity lifecycle.
In order to change the text of the text view, simply move the textTexter method from Texter to TextActivity and call it.
Remove the Texter activity as it's unused.
EDIT: Just noticed you want to do it from another class. To do that, simply give a reference to the text view you want to change and setText on it