gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 18:20:15 25 4
gpt4 key购买 nike

我尝试使用以下 3 个变量:

public boolean mDetailsExpanded;

public int mHeightBefore;

public String mHeaderItem;

但是当我将它们分配给字符串、 boolean 值和整数时:

 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;

}

然后尝试稍后保存它们:

  @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);

}

由于超出范围,它们不断返回 null。我如何纠正这个问题,将它们置于 onSaveInstanceState 的范围内,从而保留它们的值?

最佳答案

setMessageDetailsExpanded中,您正在创建局部变量而不是分配给成员变量。将其更改为:

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;

}

关于java - 非常基本的变量超出范围问题 - Java Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27826395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com