display maps v2 in a viewpage fragment NullPointerException
<p>i am trying since two or three days to implement google map v2 in a view page. i only get NullPointerException errors. i dont know what i am doing wrong. what i want is two swipe between 2 fragments, which one of them is a map. the another fragment is only a textview (is not important yet).</p>

<p>here is my code:</p>

<pre><code>public class MainActivity extends FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;

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

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the app.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}
</code></pre>

<p>the FragmentPagerAdapter:</p>

<pre><code>public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int position) {

    if (position &lt; 1) {
        Test_Fragment f = new Test_Fragment(position);
        return f;
    }
    else {
        Fragment_Map f = new Fragment_Map(position);
        return f;
    }
}

@Override
public int getCount() {
    // Show 2 total pages.
    return 2;
}

@Override
public CharSequence getPageTitle(int position) {
    switch (position) {
    case 0:
        return "Test Fragment";
    case 1:
        return "Map Fragment";
    }
    return null;
}
      }
</code></pre>

<p>the MapFragment:</p>

<pre><code>public class Fragment_Map extends Fragment {

GoogleMap mMap;

private int fragmentNR;

public Fragment_Map(int nr) {
    this.fragmentNR = nr;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

        if (mMap != null) {
            setUpMap();
        }
    }
    return (RelativeLayout) inflater.inflate(R.layout.fragment_map, container, false);
}


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

<p>the main.xml looks like </p>

<pre><code>&lt;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=".MainActivity" &gt;

&lt;android.support.v4.view.PagerTitleStrip
    android:id="@+id/pager_title_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#33b5e5"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="#fff" /&gt;

    &lt;/android.support.v4.view.ViewPager&gt;
</code></pre>

<p>and finally the fragmentmap.xml</p>

<pre><code>     &lt;?xml version="1.0" encoding="utf-8"?&gt;
     &lt;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"&gt;

      &lt;fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/&gt;

       &lt;/RelativeLayout&gt;
</code></pre>

<p>the error-massage from logcat shows me:</p>

<pre><code>     01-08 15:16:11.726: E/AndroidRuntime(781): FATAL EXCEPTION: main
     01-08 15:16:11.726: E/AndroidRuntime(781): java.lang.NullPointerException
     01-08 15:16:11.726: E/AndroidRuntime(781):  atswipe.views2.Fragment_Map.onCreateView(Fragment_Map.java:39)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.view.View.measure(View.java:15172)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
     01-08 15:16:11.726: E/AndroidRuntime(781):     at android.view.View.measure(View.java:15172)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.View.measure(View.java:15172)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.View.measure(View.java:15172)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer.doFrame(Choreographer.java:525)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.os.Handler.handleCallback(Handler.java:615)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.os.Handler.dispatchMessage(Handler.java:92)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.os.Looper.loop(Looper.java:137)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at android.app.ActivityThread.main(ActivityThread.java:4745)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at java.lang.reflect.Method.invokeNative(Native Method)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at java.lang.reflect.Method.invoke(Method.java:511)
    01-08 15:16:11.726: E/AndroidRuntime(781):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    01-08 15:16:11.726: E/AndroidRuntime(781): at    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    01-08 15:16:11.726: E/AndroidRuntime(781): at dalvik.system.NativeStart.main(Native Method)
    01-08 15:21:11.768: I/Process(781): Sending signal. PID: 781 SIG: 9
</code></pre>

<p>how can i fix that problem? any ideas?</p>

<p>thanks in advance</p>
