gpt4 book ai didi

java - 当我使用 Wea​​kReference 时,无法解析 android 上的符号消息

转载 作者:搜寻专家 更新时间:2023-11-01 08:24:12 24 4
gpt4 key购买 nike

我的应用相机预览记录应用。我用 ArrayList , 在录制相机预览期间。

ArrayList在全局变量上声明

private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInput>();

当我记录停止按钮点击时,执行stop()方法

@Override
public void stop() {
pairs.clear();
pairs = null;
stopped = true;
}

所以,如果我继续录制而不单击录制停止按钮。发生大量内存泄漏。

enter image description here

所以,我想使用 WeakReference我试试这个

//private ArrayList<OutputInputPair> pairs = new ArrayList<OutputInputPair();
private ArrayList<WeakReference<OutputInputPair>> pairs = new ArrayList<WeakReference<OutputInputPair>>(); //global variable

@Override
public void add(OutputInputPair pair) {
//pairs.add(pair);
pairs.add(new WeakReference<OutputInputPair>(pair));
}

@Override
public void stop() {
pairs.clear();
pairs = null;
stopped = true;
}

@Override
public void process() { //record method
//for (OutputInputPair pair : pairs) {
for (WeakReference<OutputInputPair> pair = pairs) {
pair.output.fillCommandQueues(); //output is cannot resolve symbol message
pair.input.fillCommandQueues(); //input is cannot resolve symbol message
}

while (!stopped) { //when user click stop button, stopped = true.
//for (OutputInputPair pair : pairs) {
for (WeakReference<OutputInputPair> pair : pairs) {
recording(pair); //start recording
}
}
}

public interface IOutputRaw { //IInputRaw class same code.
void fillCommandQueues();
}

我认为如何避免内存力下降,使用WeakReference是吗?

如何修复无法解析符号消息使用弱引用?

谢谢。

public class OutputInputPair {
public IOutputRaw output;
public IInputRaw input;

public OutputInputPair(IOutputRaw output, IInputRaw input) {
this.output = output;
this.input = input;
}
}

最佳答案

我对 WeakReference 了解不多。但是您应该使用 get() 方法来获取实际引用。

使用:

if(pair == null) continue;
OutputInputPair actualPair = pair.get();
if(actualPair == null) continue;
actualPair.output.fillCommandQueues();
actualPair.input.fillCommandQueues();

代替:

pair.output.fillCommandQueues();
pair.input.fillCommandQueues();

关于java - 当我使用 Wea​​kReference 时,无法解析 android 上的符号消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46944666/

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