Java 当我使用WeakReference时,无法在android上解析符号消息

Java 当我使用WeakReference时,无法在android上解析符号消息,java,android,arraylist,weak-references,Java,Android,Arraylist,Weak References,我的应用程序相机预览记录应用程序。 在录制相机预览期间,我使用了ArrayList ArrayList在全局变量上声明 private ArrayList pairs=new ArrayList() 当我记录停止按钮点击时,执行stop()方法 @Override public void stop() { pairs.clear(); pairs = null; stopped = true; } 因此,如果我继续录制而不单击录制停止按钮。 发生大量内存泄漏 所以,我想使用

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

ArrayList
在全局变量上声明

private ArrayList pairs=new ArrayList()

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

@Override
public void stop() {
   pairs.clear();
   pairs = null;
   stopped = true;
}
因此,如果我继续录制而不单击录制停止按钮。 发生大量内存泄漏

所以,我想使用
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
了解不多。但是您应该使用
get()
方法来获取实际引用

使用:

而不是:

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

null
使用前检查是必需的。不,不是对,
pair.get()
如果弱引用对象被清除,则可以返回null。@Oleg-oops,再次编辑,正如我提到的,我对
WeakReference
不太了解。无需担心。@Oleg
pair==null
?支票?很高兴我能帮忙。:)
if(pair == null) continue;
OutputInputPair actualPair = pair.get();
if(actualPair == null) continue;
actualPair.output.fillCommandQueues();
actualPair.input.fillCommandQueues();
pair.output.fillCommandQueues();
pair.input.fillCommandQueues();