这是我无法弄清的另一个错误。
我有这个班ExtendedLocation extends Location
被实例化时抛出ClassCastException
currentGpsLocation = 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)
ExtendedLocation.class
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);
}
}
最佳答案
onLocationChanged
由LocationManager
调用。该对象由该管理器传递,不是您的类的对象。而不是像您那样拥有构造函数:
public ExtendedLocation(ExtendedLocation l) {
super(l);
}
尝试将其替换为:
public ExtendedLocation(Location l) {
super(l);
}
这是来自
onLocationChanged
的行 currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);
变成:
currentGpsLocation = new ExtendedLocation(location);