类中的 TextView 和 setText

发布于 2024-12-02 03:08:09 字数 1160 浏览 0 评论 0原文

我知道有人对此有一些疑问,但我仍然不明白。 我有一个活动

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2024-12-09 03:08:09

您正在实例化一个活动子类,但您不应该这样做......
另外,您尝试在上下文为空时查找 ViewById (在 Texter 类中),因为您尚未经历正确的 Activity 生命周期。

为了更改文本视图的文本,只需将 textTexter 方法从 Texter 移动到 TextActivity 并调用它即可。
删除未使用的 Texter 活动。

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);
       TextView tv = (TextView) findViewById(R.id.myTextViewInXml);
       tv.setText(text);


   }

}

编辑:刚刚注意到你想从另一个班级做这件事。为此,只需提供对要更改的文本视图的引用并对其进行 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.

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);
       TextView tv = (TextView) findViewById(R.id.myTextViewInXml);
       tv.setText(text);


   }

}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文