monodroid-samples icon indicating copy to clipboard operation
monodroid-samples copied to clipboard

ActionBarViewPager sample throws exception when orientation is changed

Open rjygraham opened this issue 10 years ago • 4 comments

While running the ActionBarViewPager (HelloSwipeViewWithTabs) sample an exception is thrown when the orientation is changed.

The issue occurs base.OnCreate(bundle); is executed when Activity1 is recreated due to the orientation change:

using System;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Support.V4.View;
using Android.Support.V4.App;

namespace HelloSwipeViewWithTabs
{
    [Activity(Label = "HelloSwipeViewWithTabs", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : FragmentActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;

            var pager = FindViewById<ViewPager>(Resource.Id.pager);
            var adaptor = new GenericFragmentPagerAdaptor(SupportFragmentManager);

            adaptor.AddFragmentView((i, v, b) =>
            {
                var view = i.Inflate(Resource.Layout.tab, v, false);
                var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView1);
                sampleTextView.Text = "This is content";
                return view;
            }
            );

            adaptor.AddFragmentView((i, v, b) =>
            {
                var view = i.Inflate(Resource.Layout.tab, v, false);
                var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView1);
                sampleTextView.Text = "This is more content";
                return view;
            }
            );

            pager.Adapter = adaptor;
            pager.SetOnPageChangeListener(new ViewPageListenerForActionBar(ActionBar));

            ActionBar.AddTab(pager.GetViewPageTab(ActionBar, "Tab1"));
            ActionBar.AddTab(pager.GetViewPageTab(ActionBar, "Tab2"));
        }
    }
}

I've based a portion of an app I'm currently developing on this sample and although my app doesn't require landscape orientation, I'd still like to understand why this is throwing and the proper way to avoid it...

rjygraham avatar Jun 23 '14 04:06 rjygraham

@kallisto potential for max?

bryancostanich avatar Jun 24 '14 19:06 bryancostanich

Im having the trouble of figuring out how to get around this as my application needs to be in landscape also and the changing just throws null pointer. If there is anyone that knows how to fix it please let me know.

pjwelcome avatar Aug 11 '14 10:08 pjwelcome

Thanks for reporting this issue. This issue is fixed on latest updated sample. Hence closing this issue.

Thanks!

Abhishekk360 avatar Jun 28 '16 18:06 Abhishekk360

Awesome, thanks for the update!

rjygraham avatar Jul 05 '16 11:07 rjygraham