2010-12-09 41 views
1

已經導入弱引用但編譯器找不到符號,什麼錯? DumpReceiver.java中存在內存泄漏我認爲弱引用可能在使用後釋放?WeakReference符號找不到

import java.lang.ref.WeakReference; 

Receiver r = new DumpReceiver(System.out); 
WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r); 


MidiInDump.java:64: cannot find symbol 
symbol : constructor WeakReference(javax.sound.midi.Receiver) 
location: class java.lang.ref.WeakReference<DumpReceiver> 
       WeakReference<Receiver> wr = new WeakReference<DumpReceiver>(r); 

              ^

回答

4

仔細查看錯誤消息。這不是在談論班級(它發現這很好)。它是在談論構造函數。它找不到類型爲WeakReference<DumpReceiver>javax.sound.midi.Receiver參數的構造函數。查看WeakReference<T>的JavaDoc,有一個構造函數採用類型爲T的參數。

您正試圖創建WeakReference<DumpReceiver>,但嘗試傳入javax.sound.midi.Receiver類型的對象。您或者需要創建一個WeakReference<Receiver>,或者將變量r更改爲DumpReceiver

+0

我同意(+1)。這是IDE告訴你的那種事情。 – 2010-12-09 08:24:56