我在使用Java平臺在Android平臺上實現自己的觀察器時遇到了一些麻煩。Java觀察器問題
我創建了一個類調用NetworkPathJni,它使用一個名爲NetworkPathJniObserver的Observer接口類來通知其他對象的更改。
這裏是NetworkPathJni.java
public class NetworkPathJni {
NetworkPathJniObserver networkPathJniObserver;
public NetworkPathJni(NetworkPathJniObserver aObserver){
networkPathJniObserver = aObserver;
Log.d("Phone", "NetworkPathJni Created");
}
public void NetworkPathStateChanged(int aAvailabilityState){
Log.d("Phone", "NetworkPathStateChanged new state = " + aAvailabilityState);
TAvailabilityState availabilityState = intToAvailability(aAvailabilityState);
Log.d("Phone", "Is SipNetworkPath alive?" + networkPathJniObserver.isAlive());
networkPathJniObserver.NetworkPathStateChanged(availabilityState);
Log.d("Phone", "NetworkPathStateChanged end");
Log.d("Phone", "Is SipNetworkPath alive? (2)" + networkPathJniObserver.isAlive());
}
的代碼,這裏是觀察者
public interface NetworkPathJniObserver {
void NetworkPathStateChanged(TAvailabilityState aAvailabilityState);
boolean isAlive();
}
代碼觀察員是在一個名爲SipNetworkPath
類如下實施
public class SipNetworkPath implements NetworkPathInterface, NetworkPathJniObserver{
NetworkPathObserverInterface observer;
NetworkPathJni networkPathJni;
public SipNetworkPath(NetworkPathObserverInterface aObserver){
domainType = aDomainType;
observer = aObserver;
networkPathJni = new NetworkPathJni(this);
Log.d("Phone", "SipNetworkPath created");
}
//NetworkPathJniObserver
@Override
public void NetworkPathStateChanged(TAvailabilityState availabilityState) {
Log.d("SipNetworkPath", "SipNetworkPath - Do networkpathstate changed!");
}
@Override
public boolean isAlive() {
return true;
}
而且SipNetworkPath如下
public class WifiNetworkPath extends SipNetworkPath{
public WifiNetworkPath(NetworkPathObserverInterface aObserver) {
super(aObserver);
}
的日誌記錄實例化表明,無論NetworkPathJni和SipNetworkPath得到創建,並淨調用workPathStateChanged(int aAvailabilityState)。
在該方法中,所有日誌記錄都會返回,但該方法不會在觀察者中被調用,並且當我詢問「SipNetworkPath是否處於活動狀態?在日誌中。
觀察員類是否失去了參考或某些東西,或者在我這樣做的過程中是否有錯誤?
這是非常多的代碼,你不能提供一個更小的例子嗎?如果我能夠複製和粘貼以編譯這些垃圾代碼,會更容易。我不能看到一個錯誤,但它應該很容易找到我的IDE的幫助。順便說一下,該EVER應該如何返回false:public boolean isAlive(){return true;} – atamanroman 2010-07-14 10:46:09
我不知道我能做到多簡單。在我看來,這不完全是一大塊代碼。這就是我的問題,爲什麼它會返回錯誤?這與丟失引用的對象或垃圾收集器收集的東西有關嗎? – 2010-07-14 10:56:33
如果你問過這樣的話:「爲什麼返回true返回false」,我不必搜索整個代碼中的錯誤 - 如果返回true不會返回true,而是程序邏輯被破壞。如果您曾嘗試提供此問題的簡短示例(http://www.yoda.arachsys.com/csharp/complete.html),則您會發現這可能與您自己的觀察者模式無關。 – atamanroman 2010-07-14 12:10:26