首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ClassCastException类扩展android.location.Location

ClassCastException类扩展android.location.Location
EN

Stack Overflow用户
提问于 2013-08-27 08:38:48
回答 5查看 798关注 0票数 1

这是另一个我搞不懂的bug。

我有一个类ExtendedLocation extends Location,它在被currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);实例化时抛出ClassCastException

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

我在java/android方面很新,所以对一些人来说,这可能是一个简单的问题,只是对我来说不是。

异常

代码语言:javascript
复制
: 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

代码语言:javascript
复制
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;
    }
}

违规代码

代码语言:javascript
复制
    @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);
        }
    }
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-08-27 08:44:19

onLocationChangedLocationManager调用。对象由管理器传递,它不是类的对象。这里没有像您这样的构造函数:

代码语言:javascript
复制
public ExtendedLocation(ExtendedLocation l) {
    super(l);
}

试着将其替换为:

代码语言:javascript
复制
public ExtendedLocation(Location l) {
    super(l);
}

这条线来自onLocationChanged

代码语言:javascript
复制
  currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);

变成:

代码语言:javascript
复制
  currentGpsLocation = new ExtendedLocation(location);
票数 1
EN

Stack Overflow用户

发布于 2013-08-27 08:43:03

ExtendedLocation构造函数的参数更改为Location,并从调用以下内容中删除类转换:currentGpsLocation = new ExtendedLocation(location);

票数 0
EN

Stack Overflow用户

发布于 2013-08-27 08:43:15

您正在尝试将Location转换为ExtendedLocation

代码语言:javascript
复制
currentGpsLocation = new ExtendedLocation((ExtendedLocation) location);
                                          ^^^^^^^^^^^ here ^^^^^^^^^^

这是不允许的。ExtendedLocationLocation。相反,情况并非如此:Location不是ExtendedLocation。因此,不能将Location传递给ExtendedLocation

不过,你不需要这个。你必须改变两个点:

将上述行更改为

代码语言:javascript
复制
currentGpsLocation = new ExtendedLocation(location);

并将类ExtendedLocation的构造函数更改为接受Location而不是ExtendedLocation

代码语言:javascript
复制
public ExtendedLocation(Location l) {
    super(l);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18460768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档