猿问

在 MainActivity 中初始化一个对象,然后全局访问它

我想在我的 MainActivity 中创建一个对象,但我想全局访问它,所以假设我有这个类


public class User {


private String description;

private String display_name;

private String email;

private String followers;

private String following;

private String posts;

private String profile_photo;

private String user_id;

private String username;


public User(){}



public User(String description, String display_name, String email, String followers, String following, String posts, String profile_photo, String user_id, String username) {

    this.description = description;

    this.display_name = display_name;

    this.email = email;

    this.followers = followers;

    this.following = following;

    this.posts = posts;

    this.profile_photo = profile_photo;

    this.user_id = user_id;

    this.username = username;

}


public String getDescription() {

    return description;

}


public void setDescription(String description) {

    this.description = description;

}


public String getDisplay_name() {

    return display_name;

}


public void setDisplay_name(String display_name) {

    this.display_name = display_name;

}


public String getEmail() {

    return email;

}


public void setEmail(String email) {

    this.email = email;

}


public String getFollowers() {

    return followers;

}


public void setFollowers(String followers) {

    this.followers = followers;

}


public String getFollowing() {

    return following;

}


public void setFollowing(String following) {

    this.following = following;

}


public String getPosts() {

    return posts;

}


public void setPosts(String posts) {

    this.posts = posts;

}


public String getProfile_photo() {

    return profile_photo;

}


public void setProfile_photo(String profile_photo) {

    this.profile_photo = profile_photo;

}


public String getUser_id() {

    return user_id;

}


public void setUser_id(String user_id) {

    this.user_id = user_id;

}


public String getUsername() {

    return username;

}


public void setUsername(String username) {

    this.username = username;

                '}';

    }

}


森栏
浏览 203回答 2
2回答

守着一只汪

我建议你按照下面的程序。您可以在应用程序的整个生命周期中创建全局变量保持值,而不管运行哪个活动。1.扩展基本的Application类扩展基android.app.Application类并添加成员变量,如下所示:&nbsp; &nbsp; public class myUser extends Application {&nbsp; &nbsp; private String description;&nbsp; &nbsp; ...&nbsp; &nbsp; public String getDescription() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return description;&nbsp; &nbsp; }&nbsp; &nbsp; public void setDescription(String description) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.description = description;&nbsp; &nbsp; }&nbsp; &nbsp; ...&nbsp; &nbsp;}2.在Manifest中声明:在您的 android 中,manifest您必须声明实现类android.app.Application(将android:name=".myUser"属性添加到现有应用程序标签):<application&nbsp;&nbsp; android:name=".myUser"&nbsp;&nbsp; android:icon="@drawable/my_icon"&nbsp;&nbsp; android:label="@string/app_name">3. 在活动中设置/获取变量在你的活动,你可以片段get和set像这样的变量:// initialise your user variable// set description variable((myUser) this.getApplication()).setDescription("your description");// get description variableString s = ((myUser) this.getApplication()).getDescription();希望它会帮助你。
随时随地看视频慕课网APP

相关分类

Java
我要回答