简体   繁体   中英

Global variables not passing properly

I have created a class that holds common variables and functions and is inherited by the activity classes that interface with the different UI pages in my app. I have been passing information between classes and activities using getVariable() and setVariable(input ) functions. Suddenly, I can no longer pass information this way (it had been working well until recent edits, and now I can't figure out which change screwed this up). I have used Log outputs to determine that the data is storing properly - with the setVariable(input) functions - but when called later with the getVariable() functions it returns null. Any thoughts?

*Note, I recently started incorporating fragments into my project, extending FragmentActivity instead of Activity on my main class. I don't think this is causing the problem, but could it? If it does, whats the best practice to pass global variable info, and use fragments?

Code samples:

Main Inherited class:

public class MenuBarActivity extends FragmentActivity {
private String keyA;
private String keyB;
private int token;
private String Salt;
private long expires;

public String getKeyB() {
    return keyB;
}

public String getKeyA() {
    return keyA;
}

public int getTokenID() {
    return token;
}

public void setToken(int tkn) {
    token = tkn;
}

public void setKeyB(String kyB) {
    keyB = kyB;
}

public void setKeyA(String kyA) {
    keyA = kyA;
}

//Other common functions
}

LogIn Activity Class (gets log in info from web, stores into global variables):

public class WebContentGet extends MenuBarActivity{

public int tryLogOn(String uEmail, String pw) {
    //call to get new keys on start up
    JSONObject jObSend = new JSONObject();
    try {
        jObSend.put("email", uEmail);
        jObSend.put("password", pw);
        t.start();
        t.join();
        if(getStatus() == USER_STATUS_SUCCESSFULLOGIN){
            String data = getData();
            JSONObject jObReturn = new JSONObject(data);
            String kyA = jObReturn.getString("keyA");
            String kyB = jObReturn.getString("keyB");
            int tkn = Integer.parseInt(jObReturn.getString("tokenID"));
            String salt = jObReturn.getString("salt");
            long exp = Long.parseLong(jObReturn.getString("expiration"));
            int uID = Integer.parseInt(jObReturn.getString("userID"));
// Log outputs confirm data being read properly, and reported to setX() functions
            setToken(tkn);
            setKeyA(kyA);
            setKeyB(kyB);
            setSalt(salt);
            setExpires(exp);
            Log.d("WebContentGet tryLogIn","login values stored");

        }

        return getStatus();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return getStatus();
    }

}

Activity Class, checks if keyA/B/etc stored:

public class UserLogIn2 extends MenuBarActivity implements EmailListener {
String emailIn;
String pwIn;
Context context = this;

@Override
public void onEmailLogInClick(String email, String pw) {
    Log.d("UserLogin2", "onEmailLogInClick");
    emailIn = email;
    pwIn = pw;
    emailIn = emailIn.trim();
    emailIn = emailIn.toUpperCase();
    Log.d("prepped email", emailIn);

    pwIn = pwIn.trim();
    WebContentGet webOb = new WebContentGet();
    int webLog = webOb.tryLogOn(emailIn, pwIn);
    if (webLog == USER_STATUS_SUCCESSFULLOGIN) {
        int tkn = getTokenID();
        long exp = getExpires();
        String kya = getKeyA();
        String kyb = getKeyB();
        String slt = getSalt();
        Log.d("UserLogIn2 - token", String.valueOf(tkn));
//Log statements confirm that getX() functions returning null
        session.storeLoginSession(emailIn, pwIn, thisUser, tkn, exp, kya, kyb, slt);

            Intent intent1 = new Intent(context, MainActivitiy.class);
            startActivity(intent1);

    } else {
        showDialog(this, "Log in failure", "Incorrect Password");
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userlogin2);

}
}

This cannot work, because you have two differend instances of your MenuBarActivity. Also that is not the way to pass data from one activity to another in android.

If you want to use data from one activity in another activity, you have to add them to an intent in the activity which provides the data, and extract them in the other. For more information see here: How do I pass data between Activities in Android application?

If you don't want to start the activity and send the data with the intent, you have to store the data somewhere eg SharedPreferences and fetch them again: How to use SharedPreferences in Android to store, fetch and edit values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM