Cuarcuiu
Cuarcuiu

Reputation: 527

Google Maps V2 into fragment cause cast error

I have created an app that view the google v2 maps succesfully into fragment; I see the map.

If I try to add this code:

MapView mapView = (MapView) inflatedView .findViewById(R.id.map);

i get this error

android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.gms.maps.MapView

My file Fragment5.java is:

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment5 extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
        {
        View inflatedView  =  inflater.inflate(R.layout.fragment_5, container,false);

        // This next line is the problem
        MapView mapView = (MapView) inflatedView .findViewById(R.id.map);

        return inflatedView;
        }
    }

My layout file is

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xxxxxxx.yyyyyyyy.MainActivity$PlaceholderFragment" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/section_label"
            android:text="text title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />


        <fragment
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            class="com.google.android.gms.maps.SupportMapFragment" />



    </LinearLayout>
</RelativeLayout>

Detailed error:

02-24 21:54:37.657: E/AndroidRuntime(19225): FATAL EXCEPTION: main
02-24 21:54:37.657: E/AndroidRuntime(19225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxxx.yyyyyyyy/com.xxxxxxx.yyyyyyyy.MainActivity}: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.gms.maps.MapView
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.os.Looper.loop(Looper.java:137)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.ActivityThread.main(ActivityThread.java:4921)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at java.lang.reflect.Method.invoke(Method.java:511)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at dalvik.system.NativeStart.main(Native Method)
02-24 21:54:37.657: E/AndroidRuntime(19225): Caused by: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.gms.maps.MapView
02-24 21:54:37.657: E/AndroidRuntime(19225):    at com.xxxxxxx.yyyyyyyy.Fragment5.onCreateView(Fragment5.java:32)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.Activity.performStart(Activity.java:5216)
02-24 21:54:37.657: E/AndroidRuntime(19225):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
02-24 21:54:37.657: E/AndroidRuntime(19225):    ... 11 more

someone could tell where he is the problem? very very very thanks :-)

Upvotes: 0

Views: 953

Answers (2)

freddiev4
freddiev4

Reputation: 2621

You should not write:

inflatedView.findViewById();

Instead, you should write getView().findViewById();

Also, I recommend using this code in order to setup the MapView:

public class Fragment5 extends Fragment {
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View inflatedView  =  inflater.inflate(R.layout.fragment_5, container,false);

        return inflatedView;
        } 

    private void setUpMapIfNeeded() {

            MapsInitializer.initialize(getActivity());

            if (googleMap == null) {
                googleMap = ((MapView) getView().findViewById(R.id.store_map_frag)).getMap();
                if (googleMap != null) {
                    setUpMap();
                }
            }
        }

        private void setUpMap() {
            googleMap.getUiSettings().setMyLocationButtonEnabled(false);
            googleMap.getUiSettings().setZoomControlsEnabled(true);
            googleMap.setMyLocationEnabled(true);

        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            // Here is where you get the view and display it in the fragment
            mapView = (MapView) getView().findViewById(R.id.store_map_frag);
            mapView.onCreate(savedInstanceState);

            setUpMapIfNeeded();

        }
   }

And if you're not already using a NavigationDrawer to setup the fragment itself, then you should call on FragmentManager and the subsequent code (FragmentTransaction, editor, etc) to add in the fragment, like android_Muncher states in his answer.

Upvotes: 1

android_Muncher
android_Muncher

Reputation: 1057

Try using supportmapfragment like this in your oncreate to inflate the map. Of Course your activity will have to extend FragmentActivity

 @Override
         protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_support_map_fragment);

         FragmentManager fmanager = getSupportFragmentManager();
                Fragment fragment = fmanager.findFragmentById(R.id.map);
                SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
                GoogleMap supportMap = supportmapfragment.getMap();
    }

Upvotes: 1

Related Questions