设置和获取活动的用户名 [英] Set and get username between activities

查看:72
本文介绍了设置和获取活动的用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的情景。
我有:

Here's the scenario. I have:

一个登录活动

一个SESSION在那里我有方法getUsername和setUserName()

A SESSION where I have methods getUsername() and setUsername()

一个GET活动的地方它应该得到的用户名。

A GET activity where it is supposed to get the username.

一旦在登录活动成功登录我这行code的

Upon successful login in the Login Activity I have this line of code

public class MainActivity extends Activity {
Session obj = new Session();

,然后在被称为tryLogin()我有这样的另一种方法。

and then in another method called tryLogin() i have this

obj.setUsername(mUsername);

在会话活动我有这些codeS:

In Session activity I have these codes:

public class Session extends Application {
String username;
int userid;


@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

public void setUsername(String user)
{
    this.username = user;
}
public String getUsername()
{
    return this.username;
}

和在获取用户名我此行的活动。

and in the Activity that gets the username I have this line.

    Session obj = new Session();
    username =  obj.getUsername();

这不能正常工作。但是,如果我手动设置用户名一些字符串在这样的会议活动:

This doesn't work properly. BUT if I manually set the 'username' to some string in the SESSION activity like this:

public class Session extends Application {
    String username = "manually set the string";

然后GET活动得到的用户名就好了。
所以,我认为它是安全的问题,缩小,从登录活动通过setUsername。

Then the GET Activity gets the username just fine. So I think it's safe to narrow down the issue to the setUsername from the login activity.

那我做错了?

感谢你这么多。
//
Android的初学者

Thank you so much. // Android Beginner

推荐答案

尝试:

public class Session extends Application {
     public static String username;

在您的会话活动。

这篇关于设置和获取活动的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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