0

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

}
9
  • 1
    Are you sure userListId.getUserListId() doesn't return 0 always? Commented Dec 19, 2012 at 7:21
  • getDetailsUserId() is useless in this code. Just refer to userId since it is a class instance variable. Commented Dec 19, 2012 at 7:24
  • Yes I,m sure because the database is working. The problem is just the value of the variable. Commented Dec 19, 2012 at 7:25
  • Lazy Ninja getDetailsUserId() is not useless, I'm using it as an accessor for another activity. Commented Dec 19, 2012 at 7:27
  • Also note that you are setting userId only if caller is "List" or "ProfileSaved". There is no else part of the if loop. So, it is quite possible that caller is not equal to "List" or "ProfileSaved" and thus userId is never set. Check that part, maybe by logging that the if loops are entered. Commented Dec 19, 2012 at 7:27

1 Answer 1

1

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

}

}
Sign up to request clarification or add additional context in comments.

3 Comments

misterdev Thanks! I'm making a new object instead, but the word static made the whole difference, after many hours it works :)
@shoriwa-shaun-benjamin get you reputation back. Here is my like.
You can make an Activity static ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.