Christopher Hammond
Christopher Hammond

Reputation: 31

Very Basic Variable Out of Scope Issue - Java Android

I'm attempting to use the following 3 variables:

public boolean mDetailsExpanded;

public int mHeightBefore;

public String mHeaderItem;

However when I assign them to a string, boolean and int:

 public void setMessageDetailsExpanded(MessageHeaderItem i, boolean expanded,
            int heightBefore) {
        mDiff = (expanded ? 1 : -1) * Math.abs(i.getHeight() - heightBefore);
        String mHeaderItem = i.toString();
        boolean  mDetailsExpanded = expanded;
        int mHeightBefore = heightBefore;             

    }

Then attempt to save them later:

  @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putFloat(BUNDLE_KEY_WEBVIEW_Y_PERCENT, calculateScrollYPercent());
        outState.putBoolean(getTag(), mDetailsExpanded);
        outState.putInt(getTag(),  mHeightBefore);
        outState.putString(getTag(), mHeaderItem);
        super.onSaveInstanceState(outState);

    }

They continually return null due to being out of scope. How might I correct this to bring them within the scope of onSaveInstanceState thereby holding their values?

Upvotes: 0

Views: 321

Answers (2)

atish shimpi
atish shimpi

Reputation: 5023

String mHeaderItem = i.toString();
boolean  mDetailsExpanded = expanded;
int mHeightBefore = heightBefore;             

This are local variables and its scope is method scope. that's why not accessible outside of method.

Upvotes: 0

kemitche
kemitche

Reputation: 801

In setMessageDetailsExpanded, you're creating local variables instead of assigning to your member variables. Change it to:

public void setMessageDetailsExpanded(MessageHeaderItem i, boolean expanded,
            int heightBefore) {
        mDiff = (expanded ? 1 : -1) * Math.abs(i.getHeight() - heightBefore);
        mHeaderItem = i.toString();
        mDetailsExpanded = expanded;
        mHeightBefore = heightBefore;             

}

Upvotes: 4

Related Questions