显示位置时谷歌地图出错

时间:2015-01-12 21:21:07

标签: android google-maps android-layout android-fragments google-maps-api-3

我在API 19上做了一个App android,我试图在纵向和横向上显示谷歌地图中的位置。何时加载活动我收到此错误:

    E/AndroidRuntime(16574): FATAL EXCEPTION: main
    01-12 17:03:42.180: E/AndroidRuntime(16574): java.lang.RuntimeException: Unable to start activity        ComponentInfo{com.example.octagonogps/com.example.octagonogps.Mapa}: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.android.maps.MapView
    01-12 17:03:42.180: E/AndroidRuntime(16574):    at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
    01-12 17:03:42.180: E/AndroidRuntime(16574):    at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2102)
    01-12 17:03:42.180: E/AndroidRuntime(16574):    at android.app.ActivityThread.access$600(ActivityThread.java:137)
    E/AndroidRuntime(16574):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
01-12 17:03:42.180: E/AndroidRuntime(16574):    at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime(16574):    at android.os.Looper.loop(Looper.java:137)
    E/AndroidRuntime(16574):    at android.app.ActivityThread.main(ActivityThread.java:4838)
    E/AndroidRuntime(16574):    at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(16574):    at java.lang.reflect.Method.invoke(Method.java:511)
    E/AndroidRuntime(16574):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
    E/AndroidRuntime(16574):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)
    E/AndroidRuntime(16574):    at dalvik.system.NativeStart.main(Native Method)
    E/AndroidRuntime(16574): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.android.maps.MapView
    E/AndroidRuntime(16574):    at com.example.octagonogps.Mapa.onCreate(Mapa.java:22)
    E/AndroidRuntime(16574):    at android.app.Activity.performCreate(Activity.java:5010)
    E/AndroidRuntime(16574):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    E/AndroidRuntime(16574):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)

这是我的代码

public class Mapa extends ActionBarActivity {

MapView mapView;
MapController mc;
GeoPoint p;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mapa);

    //GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    mapView = (MapView)findViewById(R.id.map);
    mapView.setBuiltInZoomControls(true);
    mapView.setSatellite(true);

    mc = mapView.getController();
    String coordinate[] = {"18.5132433333333","-72.2925283333333"};
    double lat = Double.parseDouble(coordinate[0]);
    double lng = Double.parseDouble(coordinate[1]);

    p = new GeoPoint(
            (int)(lat * 1E6),
            (int)(lng * 1E6));
    mc.animateTo(p);
    mc.setZoom(21);
    mapView.invalidate();       
}
} 

这是我的活动-------------------------------------- 这是我的活动----------------------------------------------

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />

1 个答案:

答案 0 :(得分:1)

Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to
com.google.android.maps.MapView E/AndroidRuntime(16574): at 
com.example.octagonogps.Mapa.onCreate(Mapa.java:22)

在第22行,您尝试将FrameLayout强制转换为MapView。要么在搜索地图时使用了错误的ID,要么将id放在xml中的错误元素上。