简体   繁体   English

非常基本的变量超出范围问题-Java Android

[英]Very Basic Variable Out of Scope Issue - Java Android

I'm attempting to use the following 3 variables: 我正在尝试使用以下3个变量:

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. 由于超出范围,它们不断返回null。 How might I correct this to bring them within the scope of onSaveInstanceState thereby holding their values? 我如何纠正这个问题,使它们处于onSaveInstanceState的范围内,从而保持其值?

In setMessageDetailsExpanded , you're creating local variables instead of assigning to your member variables. setMessageDetailsExpanded ,您正在创建局部变量,而不是分配给成员变量。 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;             

}
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. 这就是为什么在方法之外无法访问的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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