0

问题是我无法从 onCreate 方法内部访问 userId 变量值。我正在从两个不同的活动访问活动,所以我必须检查我来自哪个活动,我对调用者执行此操作,但 getDetailsUserId 方法返回的值始终为 0,即使 userId 是类变量.

例如,如果我用 5 初始化 userId,那是 getDetailsUserId 方法返回的值......而不是 onCreate 方法的值。

public class Details extends Activity {

    long userId;

    long getDetailsUserId(){
        return userId; //This is the variable value I can't get.
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        final String caller;

        //Get previous activity name
        caller = getIntent().getStringExtra("com.mysite.myapp.Caller");

        //Get user id from "List"
        if(caller.equals("List")){
            List userListId = new List();
            userId = userListId.getUserListId();
        }

        //Get user id from "Profile Saved"
        else if(caller.equals("ProfileSaved")){
            ProfileSaved savedUserId = new ProfileSaved();
            userId = savedUserId.getSavedUserId();
        }

}
4

1 回答 1

1

对不起,改变了我之前的答案。试过了,它对我有用。

这里是

public static class Details extends Activity {

   static long userId;

    long getDetailsUserId(){
        return userId; //This is the variable value I can't get.
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_layout);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        final String caller;

        //Get previous activity name
        caller = getIntent().getStringExtra("com.mysite.myapp.Caller");

        //Get user id from "List"
        if(caller.equals("List")){
            List userListId = new List();
            userId = userListId.getUserListId();
        }

        //Get user id from "Profile Saved"
        else if(caller.equals("ProfileSaved")){
            ProfileSaved savedUserId = new ProfileSaved();
            this.userId = savedUserId.getSavedUserId();
        }

}

}
于 2012-12-19T07:29:13.747 回答