0

So I have two fragments and am using view pager so I can have tabbed swipe views. The tabs and swiping worked fine until i started adding the map. Any suggestions to help get rid of the errors?

Here's my code:

MainActivity:

package com.nathan.trackrun;

//Import Libraries
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;

//Create main class
public class MainActivity extends FragmentActivity implements TabListener {

    //Declare ViewPager and ActionBar variables
    ViewPager viewPager;
    ActionBar actionBar;
    public static FragmentManager fragmentManager;

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

        fragmentManager = getSupportFragmentManager();
        //Initialise ViewPager
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));

        //Listen for changes in screen
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                actionBar.setSelectedNavigationItem(arg0);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

        //Setup ActionBar (the bar that holds the Tabs on it)
        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        //Create new tab called tabMap
        ActionBar.Tab tabMap = actionBar.newTab();
        //Set tab text to 'Map'
        tabMap.setText("Map");
        tabMap.setTabListener(this);

        //Create new tab called tabStats
        ActionBar.Tab tabStats = actionBar.newTab();
        //Set tab text to 'Stats'
        tabStats.setText("Stats");
        tabStats.setTabListener(this);

        //Add the new tabs to the ActionBar
        actionBar.addTab(tabMap);
        actionBar.addTab(tabStats);

    }
    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub


    }
    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

}

class MyAdapter extends FragmentPagerAdapter{

    public MyAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        Fragment fragment = null;
        if(arg0==0){
            fragment = new FragmentMap();
        }
        if(arg0==1){
            fragment = new FragmentStats();
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

}

The map fragment:

package com.nathan.trackrun;

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


import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */
public class FragmentMap extends Fragment {

    MapView mMapView;
    private GoogleMap googleMap;

    public FragmentMap() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_map, container,
                false);
        mMapView = (MapView) v.findViewById(R.id.location_map);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume();// needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        googleMap = mMapView.getMap();
        // latitude and longitude
        double latitude = 17.385044;
        double longitude = 78.486671;

        // create marker
        MarkerOptions marker = new MarkerOptions().position(
                new LatLng(latitude, longitude)).title("Hello Maps");

        // Changing marker icon
        marker.icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

        // adding marker
        googleMap.addMarker(marker);
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
        googleMap.animateCamera(CameraUpdateFactory
                .newCameraPosition(cameraPosition));

        // Perform any camera updates here
        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

    }

Main Activity XML:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nathan.trackrun.MainActivity"
    tools:ignore="MergeRootFrame" />

Fragment XML:

<FrameLayout 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"
    tools:context="com.nathan.trackrun.FragmentMap"
    android:background="#000000" >

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

</FrameLayout>

LogCat:

11-15 06:39:46.894: D/dalvikvm(2459): GC_FOR_ALLOC freed 69K, 5% free 2904K/3028K, paused 1ms, total 3ms 
11-15 06:39:46.894: I/dalvikvm-heap(2459): Grow heap (frag case) to 3.495MB for 635808-byte allocation 
11-15 06:39:46.914: D/dalvikvm(2459): GC_FOR_ALLOC freed 2K, 4% free 3523K/3652K, paused 11ms, total 11ms
11-15 06:39:47.004: I/u(2459): Making Creator dynamically 
11-15 06:39:47.004: I/Google Maps Android API(2459): Google Play services client version: 4452000 
11-15 06:39:47.004: I/Google Maps Android API(2459): Google Play services package version: 4452036 
11-15 06:39:47.044: D/dalvikvm(2459): GC_FOR_ALLOC freed 306K, 9% free 3731K/4092K, paused 23ms, total 23ms 
11-15 06:39:47.074: I/fpp(2459): Making Creator dynamically 
11-15 06:39:47.074: I/Google Maps Android API(2459): Google Play services client version: 4452000 
11-15 06:39:47.094: D/dalvikvm(2459): GC_FOR_ALLOC freed 329K, 9% free 3916K/4300K, paused 12ms, total 13ms 
11-15 06:39:47.104: D/dalvikvm(2459): GC_FOR_ALLOC freed 216K, 7% free 4140K/4412K, paused 2ms, total 2ms 
11-15 06:39:47.104: I/dalvikvm-heap(2459): Grow heap (frag case) to 4.702MB for 635808-byte allocation 
11-15 06:39:47.124: D/dalvikvm(2459): GC_FOR_ALLOC freed <1K, 6% free 4761K/5036K, paused 12ms, total 12ms 
11-15 06:39:47.144: D/AndroidRuntime(2459): Shutting down VM 
11-15 06:39:47.144: W/dalvikvm(2459): threadid=1: thread exiting with uncaught exception (group=0xb2d18b20) 
11-15 06:39:47.144: E/AndroidRuntime(2459): FATAL EXCEPTION: main 
11-15 06:39:47.144: E/AndroidRuntime(2459): Process: com.nathan.trackrun, PID: 2459 
11-15 06:39:47.144: E/AndroidRuntime(2459): java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to com.google.android.gms.maps.MapView 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at com.nathan.trackrun.FragmentMap.onCreateView(FragmentMap.java:38)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:478)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.View.measure(View.java:16497) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.View.measure(View.java:16497) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.View.measure(View.java:16497) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.View.measure(View.java:16497) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.Choreographer.doFrame(Choreographer.java:544) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.os.Handler.handleCallback(Handler.java:733) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.os.Handler.dispatchMessage(Handler.java:95) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.os.Looper.loop(Looper.java:136) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at android.app.ActivityThread.main(ActivityThread.java:5017) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at java.lang.reflect.Method.invokeNative(Native Method) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at java.lang.reflect.Method.invoke(Method.java:515) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-15 06:39:47.144: E/AndroidRuntime(2459):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
11-15 06:39:47.144: E/AndroidRuntime(2459):     at dalvik.system.NativeStart.main(Native Method) 
11-15 06:39:51.164: I/Process(2459): Sending signal. PID: 2459 SIG: 9
mjp66
  • 4,214
  • 6
  • 26
  • 31
Nathan Hoy
  • 15
  • 7

1 Answers1

0

Looks like your code is copied from this answer.

However you skipped this part:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />

And did this:

<FrameLayout 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"
    tools:context="com.nathan.trackrun.FragmentMap"
    android:background="#000000" >
<fragment
        android:id="@+id/location_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />
</FrameLayout>

Use MapView instead of a fragment.

Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116
  • Thank you very much! How would I go about changing the size of the MapView itself though? I'm pretty new to Android programming :S – Nathan Hoy Nov 15 '14 at 12:43
  • @NathanHoy specify the `layout_width` and `layout_height`, for your `MapView`. Or wrap the MapView in a FrameLayout, like in your example, and add padding to it. – Simas Nov 15 '14 at 12:46
  • I'm having trouble getting location data now. How do I go about doing this? The code hasn't changed since I started trying to get location data. – Nathan Hoy Nov 15 '14 at 15:30
  • @NathanHoy sounds like a whole different question, it doesn't belong in the comments section. – Simas Nov 15 '14 at 15:32