3

一旦建立了与网络的连接,我有一个需要执行操作的活动。我尝试实现观察者模式,但是当我尝试将观察者活动注册到主题时,我得到了 NullPointerException。有什么想法吗?

网络状态

public class NetworkStatus implements NetworkStatusSubject, Runnable {

    List<NetworkObserver> observerList;
    Context context;
    public NetworkStatus(Context context) {
        this.context = context;
    }

    public static boolean isConnectedToInternet(Context context) {

        boolean haveConnectedWifi = false;
        boolean haveConnectedMobile = false;

        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInfo = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfo) {
            if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                if (ni.isConnected())
                    haveConnectedWifi = true;
            if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                if (ni.isConnected())
                    haveConnectedMobile = true;
        }
        return haveConnectedWifi || haveConnectedMobile;
    }

    /**
     * Add observer to list
     */
    @Override
    public void addObserver(NetworkObserver o) {
        observerList.add(o);

    }

    /**
     * Remove Observer from list
     */
    @Override
    public void removeObserver(NetworkObserver o) {
        observerList.remove(o);
    }

    @Override
    public void notifyObservers() {
        for(NetworkObserver networkObserver :  observerList) {
            networkObserver.updateStatus();
        }
    }

    @Override
    public void run() {

        while(true) {
            Log.d("NetworkStatus", "Checking Network Status");
            if(isConnectedToInternet(context)){
                notifyObservers();
                Log.d("NetworkStatus", "Network Connection is established");
            }
            else {
                Log.d("NetworkStatus", "Not connected to network");
            }
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

需要通知的活动

public class OfflineActivity extends Activity implements NetworkObserver {

    public GeoPoint ourLocationGeoPoint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.offline_mode_layout);

        //Register for network status updates
        NetworkStatus networkStatus = new NetworkStatus(this);
        networkStatus.addObserver(new OfflineActivity());
        networkStatus.run();

        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {

            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location
                // provider.
                /**
                 * Save Location to geopoint
                 */
                int lat = (int) (location.getLatitude() * 1E6);
                int lng = (int) (location.getLongitude() * 1E6);
                ourLocationGeoPoint = new GeoPoint(lat, lng);
            }

            public void onStatusChanged(String provider, int status,
                    Bundle extras) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onProviderDisabled(String provider) {
            }
        };
        for (String provider : locationManager.getAllProviders()) {
            // Register the listener with the Location Manager to receive
            // location updates
            locationManager.requestLocationUpdates(provider, 0, 0,
                    locationListener);
        }
    }
    @Override
    public void updateStatus() {

        Log.d("OfflineActivity", "Connection Established, Uploading Points!");

    }
}
4

1 回答 1

2

observerList被声明为实例变量但从未实例化。

实例化它,构造函数是一个很好的地方:

public NetworkStatus(Context context) {
        this.context = context;
        observerList = new List <NetworkObserver> ();
    }
于 2013-01-31T20:04:57.200 回答