2017-10-26 5 views
1

My appカメラプレビューレコードアプリ。 録画カメラのプレビュー中にArrayListを使用します。グローバル変数WeakReferenceを使用すると、androidのシンボルメッセージを解決できません

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

とI記録停止ボタンのクリックで宣言

ArrayList、私はクリックレコード停止ボタンなしで録画を続行した場合、そのstop()方法

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

を実行します。 が多量のメモリリークを起こします。そう

enter image description here

、私はWeakReference を使用したい私は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; 
    } 
} 

答えて

2

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

'それが必要とされて使用する前に確認してくださいNULL'。 – Oleg

+0

@Olegありがとう、編集中。 –

+0

いいえ、ペアではなく、弱く参照されたオブジェクトがクリアされた場合、 'pair.get()'はnullを返します。 – Oleg

関連する問題