Android无法将Serializable捆绑到捆绑中

时间:2014-10-10 05:45:47

标签: java android serialization

我有类实现Serializable:

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.tilangapp.activity.MenuActivity

为什么?

2 个答案:

答案 0 :(得分:0)

因为某些成员不可序列化!

您还有其他选择,

  1. 编写自己的序列化方法。 see here
  2. 创建一个只包含可序列化成员并且您真正用作数据的成员的类,然后实例化并将其捆绑。

答案 1 :(得分:0)

它不可序列化。请阅读有关可序列化对象的更多信息。它必须具有非参数公共构造函数。必须使用可序列化的属性。必须遵循java bean表示法。

在任何情况下,你都无法使用!!!

中的上下文序列化这样的怪物