gpt4 book ai didi

android - ClassCastException 类扩展了 android.location.Location

转载 作者:行者123 更新时间:2023-11-30 03:24:31 32 4
gpt4 key购买 nike

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

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

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

我是 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);
}
}

最佳答案

onLocationChangedLocationManager 调用。该对象由该经理传递,它不是您类(class)的对象。而不是像你在那里那样拥有一个构造函数:

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

尝试将其替换为:

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

这行来自 onLocationChanged

  currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);

变成:

  currentGpsLocation = new ExtendedLocation(location);

关于android - ClassCastException 类扩展了 android.location.Location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460768/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com