0

I am trying to have google maps in one of the tabs of my app. I have spent the better part of two days attempting to do this but I simply cannot.

BTW I am using Android Studio. Also, I have download all google relate SDK packages.

Below is my code. I was able to have maps work in a stand alone app, so I copied the code into my tabbed app.

Java:

public class Maps extends Fragment{

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        setUpMapIfNeeded();
    }

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

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
} 

XML:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <fragment 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:id="@+id/map" tools:context=".MapsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>

Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "dolphin.dolphinapp"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.google.android.gms:play-services:7.5.0'
}

here is the error I get when I run this:

07-01 08:19:05.044      467-467/dolphin.dolphinapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: dolphin.dolphinapp, PID: 467
    java.lang.IllegalArgumentException: No view found for id 0x7f0d0066 (dolphin.dolphinapp:id/pager) for fragment Maps{55a2815 #3 id=0x7f0d0066 android:switcher:2131558502:2}
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)
            at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
            at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:494)
            at dolphin.dolphinapp.MainActivity.onTabSelected(MainActivity.java:147)
            at android.support.v7.internal.app.WindowDecorActionBar.selectTab(WindowDecorActionBar.java:640)
            at android.support.v7.internal.app.WindowDecorActionBar$TabImpl.select(WindowDecorActionBar.java:1224)
            at android.support.v7.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:568)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Thanks for your help!

JohnF
  • 23
  • 8
  • Look to see if this helps your issue http://stackoverflow.com/questions/25665642/java-lang-illegalargumentexception-no-view-found-for-id-0x1020002-androidid-c – Patrick Murphy Jul 02 '15 at 20:22
  • A quick fix woud be to use `getChildFragmentManager()` instead of `getSupportFragmentManager()`, but then you would have nested fragments, which is not optimal. – Daniel Nugent Jul 02 '15 at 20:29

0 Answers0