2014-10-10 39 views
0

我有類實現Serializable:的Android不能把序列化爲束

public class LocationUtilGPSNetwork implements Serializable 
{ 
    private Context mContext; 

    private LocationManager mLocationManager; 

    // private LocationListener mListenerPassive; 
    private LocationListener mListenerNetwork; 
    private LocationListener mListenerGPS; 

    private Location mLocation; 
    private long mTimeLimit; 

    public LocationUtilGPSNetwork(Context context) 
    { 
     mContext = context; 
     mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 


     mListenerNetwork = new LocationListener() 
     { 
      @Override 
      public void onLocationChanged(Location location) 
      { 
       updateLocation(location); 
       Log.i("Location", "Network:" + location); 
      } 

      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) 
      { 

      } 

      @Override 
      public void onProviderEnabled(String s) 
      { 

      } 

      @Override 
      public void onProviderDisabled(String s) 
      { 

      } 
     }; 

     mListenerGPS = new LocationListener() 
     { 
      @Override 
      public void onLocationChanged(Location location) 
      { 
       updateLocation(location); 
       Log.i("Location", "GPS:" + location); 
      } 

      @Override 
      public void onStatusChanged(String s, int i, Bundle bundle) 
      { 

      } 

      @Override 
      public void onProviderEnabled(String s) 
      { 

      } 

      @Override 
      public void onProviderDisabled(String s) 
      { 

      } 
     }; 
    } 

    public void startLocationListeners(long minTimeUpdate, float minDistanceUpdate, long timeLimit) 
    { 
     mTimeLimit = timeLimit; 

     mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, minTimeUpdate, 
       minDistanceUpdate, mListenerNetwork); 
     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTimeUpdate, 
       minDistanceUpdate, mListenerGPS); 
    } 


    private void updateLocation(Location location) 
    { 
     if (location != null && isBetterLocation(location, mLocation)) 
     { 
      mLocation = location; 
     } 
    } 

    private boolean isBetterLocation(Location location, Location currentBestLocation) 
    { 
     if (currentBestLocation == null) 
     { 
      // A new location is always better than no location 
      return true; 
     } 

     // Check whether the new location fix is newer or older 
     long timeDelta = location.getTime() - currentBestLocation.getTime(); 
     boolean isSignificantlyNewer = timeDelta > mTimeLimit; 
     boolean isSignificantlyOlder = timeDelta < -mTimeLimit; 
     boolean isNewer = timeDelta > 0; 

     // If it's been more than two minutes since the current location, use the new location 
     // because the user has likely moved 
     if (isSignificantlyNewer) 
     { 
      return true; 
     } 
     // If the new location is more than two minutes older, it must be worse 
     else if (isSignificantlyOlder) 
     { 
      return false; 
     } 

     // Check whether the new location fix is more or less accurate 
     int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
     boolean isLessAccurate = accuracyDelta > 0; 
     boolean isMoreAccurate = accuracyDelta < 0; 
     boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

     // Check if the old and new location are from the same provider 
     boolean isFromSameProvider = isSameProvider(location.getProvider(), 
       currentBestLocation.getProvider()); 

     // Determine location quality using a combination of timeliness and accuracy 
     if (isMoreAccurate) 
     { 
      return true; 
     } 
     else if (isNewer && !isLessAccurate) 
     { 
      return true; 
     } 
     else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) 
     { 
      return true; 
     } 

     return false; 
    } 

    private boolean isSameProvider(String provider1, String provider2) 
    { 
     if (provider1 == null) 
     { 
      return provider2 == null; 
     } 

     return provider1.equals(provider2); 
    } 

    public Location getLocation() 
    { 
     return mLocation; 
    } 

    public void stopLocationListeners() 
    { 
     mLocationManager.removeUpdates(mListenerNetwork); 
     mLocationManager.removeUpdates(mListenerGPS); 
    } 
} 

當我把它放進包:

bundle.putSerializable(MenuActivity.locationID, mLocationUtil); 

我有這個錯誤

10-10 12:27:42.970:E/AndroidRuntime(20130):由: java.io.NotSerializableException: com.pattindo.til angapp.activity.MenuActivity

爲什麼?

回答

0

因爲有些成員不可序列化!

您還有其他的選擇,

  1. 寫自己的序列化方法。 see here
  2. 創建一個只包含可序列化成員並且真正用作數據的類的類,然後實例化並將其捆綁。
0

它是不可序列化的。請閱讀有關可序列化對象的更多信息。它必須有一個非參數的公共構造函數。必須使用可序列化的屬性。必須遵循java bean符號。

在任何情況下,你都不能序列化這樣一個怪物,甚至在!

相關問題