0

i'm using provided by default in Android studio tabbed activity which i believe takes an advantage ViewPager, and extends ActionBarActivity which happens to be deprecated despite the fact it is used as default template in the newest Android Studio. However when i try to follow instructions provided here How to put Google Maps V2 on a Fragment Using ViewPager and attempt to insert google maps view into one of my tabbed fragments i run into this frankly annoying error

08-23 17:56:31.040  13735-13735/com.eggstudio.affinity E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.eggstudio.affinity, PID: 13735
    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
            at com.eggstudio.affinity.Fragment3.onViewCreated(Fragment3.java:85)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
            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.populate(ViewPager.java:951)
            at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
            at android.view.Choreographer.doCallbacks(Choreographer.java:580)
            at android.view.Choreographer.doFrame(Choreographer.java:549)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5317)
            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:1016)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

Pointing to this line of code

   mMap = ((SupportMapFragment) MainActivity.fragmentManager
            .findFragmentById(R.id.location_map)).getMap();

It is also worth noting that since my main activity looks like as shown below by default i was unable to extend it further by FragmentActivity as instructed in previously referenced post, which could cause this issue however due to my lack of experience I'm unable to bypass this issue.

public class MainActivity extends ActionBarActivity implements
        ActionBar.TabListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener
{...}
Community
  • 1
  • 1
5m0k3
  • 65
  • 1
  • 9
  • 1
    Is the SupportMapFragment in the layout of your Activity or your Fragment? Regardless, the answer here might help for your initial problem: http://stackoverflow.com/a/31461334/4409409 That being said, if you are using nested Fragments with a ViewPager, you're likely to run into other problems. I think the best approach is to create a Fragment that extends SupportMapFragment, and use that for your map tab, take a look at this answer: http://stackoverflow.com/a/31352483/4409409 – Daniel Nugent Aug 23 '15 at 18:01

2 Answers2

0

Try extending the Fragment activity directly, something like this:

 extends FragmentActivity implements GoogleMap.OnMyLocationChangeListener

I had used this while working on my app.

Let me know if it works.

  • From what i understood i have attempted extending Fragment hosting google maps by this however this results in array of errors one of them being in MainActivity and precisely in "public Fragment getItem(int position)" stating that it is unable to return Fragment3. Just in case you meant extending main activity i have tried that as well but it doesn't allow extending activity by multiple classes. – 5m0k3 Aug 23 '15 at 18:32
  • Sorry for the confusion. Yes I mean t extending the main activity but not with multiple classes. I meant this:public class MainActivity extends FragmentActivity implements GoogleMap.OnMyLocationChangeListener – Saurabh Nailwal Aug 23 '15 at 23:53
0

I faced similar issue with accessing fragments from ViewPager. In short, even if you have reference to your fragments in viewpager, they will disappear on device rotation.

What you need to do is to save your fragment into a SparseArray like this:

    //Override this method to put fragment into the SparseArray when it is initialized
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

Then you will be able to restore fragments according to their position. This will work both for FragmentStatePagerAdapter and FragmentPagerAdapter

Ivan V
  • 3,024
  • 4
  • 26
  • 36