Java Android无法将Serializable放入捆绑包中

Java Android无法将Serializable放入捆绑包中,java,android,serialization,Java,Android,Serialization,我有可序列化的类实现: public class LocationUtilGPSNetwork implements Serializable { private Context mContext; private LocationManager mLocationManager; // private LocationListener mListenerPassive; private LocationListener mListenerNetwork;

我有可序列化的类实现:

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);
    }
}
我有这个错误

10-10 12:27:42.970:E/AndroidRuntime(20130):由以下原因引起: java.io.NotSerializableException: com.pattindo.tilangapp.activity.MenuActivity


为什么???

因为有些成员不可序列化

你还有其他选择

  • 编写自己的序列化方法
  • 创建一个只包含可序列化且真正用作数据的成员的类,然后实例化并将其放入捆绑包

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

    在任何情况下,你都无法序列化这样一个怪物,甚至连上下文都在里面

    bundle.putSerializable(MenuActivity.locationID, mLocationUtil);