1

这是另一个我无法弄清楚的错误。

我有这个类在实例化时 ExtendedLocation extends Location抛出ClassCastExceptioncurrentGpsLocation = new ExtendedLocation((ExtendedLocation) location);

查看堆栈跟踪绝对不会告诉我任何可能的解决方案。也许比我聪明的人可以插话。

我对这个 java/android 的东西很陌生,所以这对某些人来说可能是一个简单的问题,而不是对我来说。


例外

: java.lang.ClassCastException: android.location.Location
:   at ru.alperez.gpsdub.GpsActivity$1.onLocationChanged(GpsActivity.java:485)
:   at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
:   at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
:   at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
:   at android.os.Handler.dispatchMessage(Handler.java:99)
:   at android.os.Looper.loop(Looper.java:130)
:   at android.app.ActivityThread.main(ActivityThread.java:3687)
:   at java.lang.reflect.Method.invokeNative(Native Method)
:   at java.lang.reflect.Method.invoke(Method.java:507)
:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
:   at dalvik.system.NativeStart.main(Native Method)

扩展位置类

package ru.alperez.model;

import org.json.JSONException;
import org.json.JSONObject;

import android.location.Location;

public class ExtendedLocation extends Location{

    public ExtendedLocation(ExtendedLocation l) {
        super(l);
    }

    public ExtendedLocation(String provider) {
        super(provider);
    }

    public JSONObject toJSON() throws JSONException {
        JSONObject ret = new JSONObject();
        ret.put("accuracy", this.getAccuracy());
        ret.put("altitude", this.getAltitude());
        ret.put("bearing", this.getBearing());
        ret.put("lat", this.getLatitude());
        ret.put("lon", this.getLongitude());
        ret.put("provider", this.getProvider());
        ret.put("speed", this.getSpeed());
        ret.put("time", this.getTime());
        return ret;
    }

    public static ExtendedLocation fromJSON(JSONObject jLocation) {
        ExtendedLocation ret = null;
        try {
            ret = new ExtendedLocation(jLocation.optString("provider"));
            ret.setAccuracy((float) jLocation.optDouble("accuracy"));
            ret.setAltitude(jLocation.optDouble("altitude"));
            ret.setBearing((float) jLocation.optDouble("bearing"));
            ret.setLatitude(jLocation.optDouble("lat"));
            ret.setLongitude(jLocation.optDouble("lon"));
            ret.setSpeed((float) jLocation.optDouble("speed"));
            ret.setTime(jLocation.optLong("time"));
        } catch (Exception e) {
            ret = null;
        }
        return ret;
    }
}

违规代码

    @Override
    public void onLocationChanged(Location location) {
        Log.d(TAG, "LocationListener::onLocationChanged: "+location);
        try
        {
            currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);
            Log.d(TAG, "LocationListener::onLocationChanged:currentGpsLocation "+currentGpsLocation);
        }
        catch (ClassCastException e)
        {
            Log.d(TAG,"ExtendedLocation failed.",e);
            return;
        }
        if (!USE_NMEA) {
            populateCurrentGpsData(currentGpsLocation, lastFixState, referenceGpsPoint);
        }
    }
4

5 回答 5

1

onLocationChanged由 调用LocationManager。该对象由该经理传递,它不是您班级的对象。而不是像你那样拥有一个构造函数:

public ExtendedLocation(ExtendedLocation l) {
    super(l);
}

尝试将其替换为:

public ExtendedLocation(Location l) {
    super(l);
}

这条线来自onLocationChanged

  currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);

变成:

  currentGpsLocation = new ExtendedLocation(location);
于 2013-08-27T08:44:19.487 回答
0

更改ExtendedLocationto的构造函数的参数Location并从调用 this 中删除类转换: currentGpsLocation = new ExtendedLocation(location);

于 2013-08-27T08:43:03.777 回答
0

您正在尝试将 aLocation转换为ExtendedLocation

currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);
                                          ^^^^^^^^^^^ here ^^^^^^^^^^

这是不允许的。一个ExtendedLocation是一个Location。反过来却不是这样:ALocation不是ExtendedLocation. 因此, aLocation不能转换为 a ExtendedLocation

但是,您不需要这个。您必须更改两个位置:

将上面的行更改为

currentGpsLocation = new ExtendedLocation(location);

并将类的构造函数更改ExtendedLocation为采用 aLocation而不是 a ExtendedLocation

public ExtendedLocation(Location l) {
    super(l);
}
于 2013-08-27T08:43:15.623 回答
0

您的 ExtendedLocation 应该将 Location 类对象作为构造函数中的参数。您也不能将位置对象转换为 ExtendLocation。

于 2013-08-27T08:44:40.853 回答
0

基类不应该知道派生自它们的类的任何信息,否则就会出现上面强调的问题。向下转型是一种“代码味道”,在基类中向下转型到派生类尤其“臭”。这样的设计也可能导致难以解决循环依赖。

如果您希望基类使用派生类实现,请使用模板方法模式,即在您的基类中添加一个虚拟或抽象方法,然后在派生类中覆盖和实现它。然后,您可以从基类安全地调用它。

请按照以下链接获取投射方法:

爪哇;将基类转换为派生类

于 2013-08-27T08:48:13.467 回答