monodroid-samples
monodroid-samples copied to clipboard
ActionBarViewPager sample throws exception when orientation is changed
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...
@kallisto potential for max?
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.
Thanks for reporting this issue. This issue is fixed on latest updated sample. Hence closing this issue.
Thanks!
Awesome, thanks for the update!