问题
I have an activity which needs to perform an operation once a connection to a network has been established. I tried implementing the Observer pattern but am getting a NullPointerException when I try to register the Observer activity to the subject. Any thoughts?
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();
}
}
}
}
Activity which needs notification
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!");
}
}
回答1:
observerList
is declared as an instance variable but never instantiated.
Instantiate it, the constructor is a good place for this:
public NetworkStatus(Context context) {
this.context = context;
observerList = new List <NetworkObserver> ();
}
来源:https://stackoverflow.com/questions/14634074/notify-activity-of-network-change-android