3

ネットワークへの接続が確立されたら、操作を実行する必要があるアクティビティがあります。オブザーバーパターンを実装しようとしましたが、オブザーバーアクティビティをサブジェクトに登録しようとするとNullPointerExceptionが発生します。何かご意見は?

NetworkStatus

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 に答える