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

标签 java android arraylist weak-references

我的应用相机预览记录应用。 我用 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/

相关文章:

android - 如何计算字符串中的符号并相应地拆分并在 Textview 中设置?

java - 如何将线程添加到 ArrayList

java - 如何将 ArrayList 拆分为两个新的 ArrayList?

Java:使用 split 方法添加时,字符串数组列表中出现额外的空格

java - 通过 Swing 按钮停止线程

java - 如何mock jcabi注解参数

android - 从 wordpress WP-REST API 获取图片 URL

java - 找不到与给定名称匹配的资源 : attr 'android:keyboardNavigationCluster'

java - 使用 AsyncTask 进行多项操作

java - 递归方法困惑