如何在使用CameraUpdate时修复此错误

时间:2015-03-11 12:08:28

标签: java android google-maps-api-2

使用时出现此错误:

CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 16);

我从其他帖子中看到我需要使用它  addOnGlobalLayoutListener 但我试过这个,似乎无法让它工作

 03-11 12:00:18.496  11035-11035/project.sharethefare E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: project.sharethefare, PID: 11035
    java.lang.RuntimeException: Unable to start activity ComponentInfo{project.sharethefare/project.sharethefare.Share}: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.LinearLayout
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5834)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
     Caused by: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.LinearLayout
            at project.sharethefare.Share.setUpMap(Share.java:105)
            at project.sharethefare.Share.setUpMapIfNeeded(Share.java:82)
            at project.sharethefare.Share.onCreate(Share.java:53)
            at android.app.Activity.performCreate(Activity.java:6221)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
            at android.app.ActivityThread.access$900(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5834)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
03-11 12:00:21.816  13207-13207/project.sharethefare E/Zygote﹕ MountEmulatedStorage()
03-11 12:00:21.816  13207-13207/project.sharethefare E/Zygote﹕ v2

我收到错误的代码部分是:

 private void setUpMap() {
        final List <Marker> markers= new ArrayList<>();
        if(alreadyInit==false){
            marker = mMap.addMarker(new MarkerOptions().position(new LatLng(home.curLat,home.curLong)).title("Marker"));
            markers.add(marker);
            alreadyInit=true;
        }

            matchLocLat = client.getlat();
            matchLocLong = client.getlong();
        if(alreadyInit==true){
            marker = mMap.addMarker(new MarkerOptions().position(new LatLng(matchLocLat, matchLocLong)).title("Marker"));
            markers.add(marker);

            LinearLayout mapLayout = (LinearLayout)findViewById(R.id.shared_map);
            mapLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    LatLngBounds.Builder builder = new LatLngBounds.Builder();
                    for (Marker marker : markers) {
                        builder.include(marker.getPosition());
                    }
                    LatLngBounds bounds = builder.build();

                    int padding = 0 ; // offset from edges of the map in pixels
                    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 16);
                    mMap.animateCamera(cu);
                }
            });


        } 

我很感激任何帮助以及我回答的任何问题

1 个答案:

答案 0 :(得分:0)

R.id.shared_map不是LinearLayout,因此您无法将其转换为LinearLayout。它与CameraUpdateFactory.newLatLngBounds(bounds, 16)无关。

由于getViewTreeObserver()是在View上实现的,因此假设R.id.shared_map真正代表您希望获得ViewTreeObserver的任何内容,一个解决方案就是摆脱强制转换对