Java 从另一个类获取字符串并将其设置为TextView

Java 从另一个类获取字符串并将其设置为TextView,java,android,android-activity,Java,Android,Android Activity,我有LoginActivity,我在这个类中声明了stringstring currentName。我有一个WelcomeActivity类,我想在文本视图中设置这个currentName。怎么做?现在,我在WelcomeActivity类中声明了LoginActivity LoginActivity并将其设置为这样,但这不起作用: TextView欢迎您; 欢迎=(TextView)findViewById(R.id.tvWelcome); welcome.setText(loginActiv

我有
LoginActivity
,我在这个类中声明了string
string currentName
。我有一个
WelcomeActivity
类,我想在文本视图中设置这个
currentName
。怎么做?现在,我在
WelcomeActivity
类中声明了
LoginActivity LoginActivity
并将其设置为这样,但这不起作用:

TextView欢迎您;
欢迎=(TextView)findViewById(R.id.tvWelcome);
welcome.setText(loginActivity.currentName)


谢谢你的帮助;)

在LoginActivity中,只需创建静态字符串

   public static String currentName= "test";
在Welcome活动中,你应该这样做

   welcome.setText(currentName);
注意-:但是使用Intent将一个活动的值传递给另一个活动的更好方法

//后勤活动

  public void sendDataToNextActivity(View view) {
    Intent intent = new Intent(this, WelcomeActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String strData= editText.getText().toString();
    intent.putExtra("data", strData);
///OR  intent.putExtra("data", "test");
    startActivity(intent);
}
//欢迎活动

  public void sendDataToNextActivity(View view) {
    Intent intent = new Intent(this, WelcomeActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String strData= editText.getText().toString();
    intent.putExtra("data", strData);
///OR  intent.putExtra("data", "test");
    startActivity(intent);
}
在onCreate中接收数据

 Bundle extras = getIntent().getExtras();
        String getDataFromFirstActivity;
        if (extras.containsKey("data") && extras != null) {
                getDataFromFirstActivity= extras.getString("data");
                welcome.setText(getDataFromFirstActivity);
            }
启动意图-


我将我的字符串设置为
公共静态字符串currentName
及其工作。谢谢大家:)

不用将currentName作为全局变量,您可以通过intent发送数据,并在欢迎活动中获取数据

在loginActivity中创建意图并发送当前名称

Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
intent.putExtra("currentNameId", currentName);
startActivity(intent);
在oncreate of welcome活动中,获取发送的数据

String currentName = getIntent().getStringExtra("currentNameId");
然后将其设置为文本视图

TextView welcome;
welcome = (TextView) findViewById (R.id.tvWelcome);
welcome.setText(currentName);

您绝对不应该存储活动的实例,因为操作系统可以并且将在某个时候重新创建它。
最简单的解决方案是将currentName声明为静态成员

public static String currentName;
之后,您可以按类名访问它:

welcome.setText(LoginActivity.currentName);
如果您从LoginActivity打开WelcomeActivity,还可以使用附加功能

intent.putExtra(SOME_TAG, currentName);
getIntent().getStringExtra(SOME_TAG);

我想最好的解决方案是将此字符串存储在中的某个位置,或者至少存储在某个位置

创建一个单例类

public class MySingleton {

    private static MySingleton mySingleton;

    private String currentName;

    public static synchronized MySingleton getInstance() {
        if (mySingleton == null)
            mySingleton = new MySingleton();
        return mySingleton;
    }

    public String getCurrentName() {
        return currentName;
    }

    public void setCurrentName(String currentName) {
        this.currentName = currentName;
    }
}
从LoginActivity集值到Singleton类

MySingleton singleton =  MySingleton.getInstance();
singleton .setCurrentName("<Name to Set>");

而不是静态变量, 你可以使用意图

Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
intent.putExtra("currentNameId", "here add your text which you want to display");
startActivity(intent);
而不是在活动中, 在onCreate方法中

String currentName = getIntent().getStringExtra("currentNameId");
现在在文本视图中设置文本

welcome.setText(currentName);

在LoginActivity中存储一个静态变量并尝试从另一个Activity访问它不是一个好主意。你必须考虑活动的生命周期和活动可能被系统破坏的事实,以及它会导致内存泄漏等。 你可以在这里阅读更多关于生命周期的内容

您要做的是在intent中作为额外值传递该值,然后在第二个活动中从intent中检索该值

在您的
LoginActivity
中,当您的用户输入了正确的凭据,并且您即将启动
WelcomeActivity
时,您希望创建启动第二个活动的意图,如下所示(将名称添加为额外名称):

然后在
WelcomeActivity
中,在
onCreate()
方法中检索它

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String currentName = getIntent().getStringExtra("extra_current_name");
    TextView welcome = (TextView) findViewById (R.id.tvWelcome);
    welcome.setText(currentName);
}

在此阅读一些关于在活动和建筑意图之间传递数据的信息,

当前的结果是什么?你有例外吗?文本是否没有改变?请添加更多信息。这意味着您在登录活动中有字符串,登录后您正在显示欢迎活动,现在您想显示登录活动中的字符串吗?将您的
字符串currentName
设置为
公共静态字符串currentName
,然后尝试您的代码。将字符串设置为活动中的静态字符串不是一个好方法。正确的方法是通过Intent传递字符串。@kima使变量成为静态变量并在其他类中访问它会导致内存泄漏。使用Intent传递数据,正如我在answer@Anonymous是的,它会导致内存泄漏,并影响活动生命周期。@Kima321这是完成所需任务的正确方法。在活动中使字符串保持静态不是一个好方法。因为它会影响活动的生命周期,所以会导致内存泄漏,因为静态变量生命周期就是应用程序的生命周期,而当您切换活动时,它的生命周期会发生变化。为了进一步研究android中的活动生命周期。@Kima你也可以按照匿名答案回答。兄弟,这可以完成任务,但这是不合适的方式。是的,兄弟,我知道,但现在我建议他使用@Anonymous answer
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String currentName = getIntent().getStringExtra("extra_current_name");
    TextView welcome = (TextView) findViewById (R.id.tvWelcome);
    welcome.setText(currentName);
}
//To send data to 2nd activity call this function or set it to button onclick.
public void onclick_next(View view) {
        Intent i = new Intent(this,2nd_activity_name.class);
        i.putExtra("username",username); //2nd username has the text that you want to send.
        startActivity(i);
    }

//When you are in second activity,  To get send data from 1st activity, use this codes inside the oncreate method.
Bundle logonData = getIntent().getExtras();
      String username = logonData.getString("username");
      final TextView usernameText = (TextView) findViewById(R.id.textViewUser);
      usernameText.setText(username);