簡體   English   中英

當我使用WeakReference時,無法在Android上解析符號消息

[英]When I use WeakReference, cannot resolve symbol message on android

我的應用程序相機預覽記錄應用程序。 我在錄制相機預覽期間使用ArrayList

在全局變量上聲明ArrayList

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

當我記錄停止按鈕單擊時,執行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是正確的嗎?

如何解決無法解決符號消息使用弱引用的問題?

謝謝。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM