如何从一个Android的onCreate方法来访问这个变量

如何从一个Android的onCreate方法来访问这个变量

本文介绍了如何从一个Android的onCreate方法来访问这个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在的问题是,我不能从onCreate方法内部访问的用户ID变量值。我访问来自两个不同的活动,活动的,所以我必须检查从中我来自哪里,我这样做,与由getDetailsUserId方法返回调用者,但值为0所有,即使用户id是一个类变量的时候

如果我初始化用户id与5,例如这是由getDetailsUserId方法的返回值...不是从onCreate方法的价值。

 公共类详情延伸活动{

    长的用户id;

    长getDetailsUserId(){
        返回用户id; //这是变量值我不能。
    }

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_details);
        getActionBar()setDisplayHomeAsUpEnabled(真)。

        最后弦乐来电;

        //获取previous活动名称
        呼叫者= getIntent()getStringExtra(com.mysite.myapp.Caller);

        //获取用户ID,从名单
        如果(caller.equals(目录)){
            名单userListId =新名单();
            用户id = userListId.getUserListId();
        }

        //获取用户ID从个人得救
        否则如果(caller.equals(ProfileSaved)){
            ProfileSaved savedUserId =新ProfileSaved();
            用户id = savedUserId.getSavedUserId();
        }

}
 

解决方案

对不起,改变了我的previous答案。尝试过了,它为我工作。

 公共静态类的详细信息扩展活动{

   静态长期用户id;

    长getDetailsUserId(){
        返回用户id; //这是变量值我不能。
    }

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.grid_layout);
        getActionBar()setDisplayHomeAsUpEnabled(真)。

        最后弦乐来电;

        //获取previous活动名称
        呼叫者= getIntent()getStringExtra(com.mysite.myapp.Caller);

        //获取用户ID,从名单
        如果(caller.equals(目录)){
            名单userListId =新名单();
            用户id = userListId.getUserListId();
        }

        //获取用户ID从个人得救
        否则如果(caller.equals(ProfileSaved)){
            ProfileSaved savedUserId =新ProfileSaved();
            this.userId = savedUserId.getSavedUserId();
        }

}

}
 

The problem is that I can't access the userId variable value from inside the onCreate method. I'm accessing the activity from two different activities so I have to check from which one I'm coming from, I do that with the caller but the value returned by the getDetailsUserId method is 0 all the time even though userId is a class variable.

If I initialize userId with 5 for example that's the value returned by the getDetailsUserId method... not the value from the onCreate method.

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();
        }

}
解决方案

Sorry, changed my previous answer. Tried it and it worked for me.

Here it is

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();
        }

}

}

这篇关于如何从一个Android的onCreate方法来访问这个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 13:31