System.NullReferenceException: Object reference not set to an instance of an object When changing ViewPager
Christopher Drosos
1
Reputation point
I'm getting this error a lot (2% of users in various android versions 7.x,8.x,9.x,10.x and devices)
FragmentOddEvenResults.ShowDrawsAsync (System.Boolean isDeleteHistoryChecked, System.Int32 timeFromPosition, System.Int32 timeToPosition)
System.NullReferenceException: Object reference not set to an instance of an object
Main thread
FragmentOddEvenResults.ShowDrawsAsync (System.Boolean isDeleteHistoryChecked, System.Int32 timeFromPosition, System.Int32 timeToPosition)
FragmentOddEvenResults.<OnCreateView>b__53_4 (System.Object <p0>, System.EventArgs <p1>)
AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state)
SyncContext+<>c__DisplayClass2_0.<Post>b__0 ()
Thread+RunnableImplementor.Run ()
IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.45(intptr,intptr)
This happens when a user clicks a button from the app menu in order to change the current view according to the Appcenter Diagnostics Event:
For changing the View i use Viewpager in this way:
//The menu code
public bool OnNavigationItemSelected(IMenuItem item)
{
// Cancel any reamining background task
ApplicationState.DisposeCancellationTokenSource();
if (item == null)
return false;
var id = item.ItemId;
var currentTab = nameof(Resource.Id.Nav_DrawSearch);
var drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
if (id == Resource.Id.Nav_DrawSearch)
{
currentTab = nameof(Resource.Id.Nav_DrawSearch);
SetCurrentViewpager(PagerAdapterTypesEnum.Search);
CurrentTabLayout.GetTabAt(0).Select();
}
else if (id == Resource.Id.Nav_DrawsPerFrequency)
{
currentTab = nameof(Resource.Id.Nav_DrawsPerFrequency);
SetCurrentViewpager(PagerAdapterTypesEnum.Search);
CurrentTabLayout.GetTabAt(1).Select();
}
else if (id == Resource.Id.Nav_DrawsPredictions)
{
currentTab = nameof(Resource.Id.Nav_DrawsPredictions);
SetCurrentViewpager(PagerAdapterTypesEnum.Search);
CurrentTabLayout.GetTabAt(2).Select();
}
else if (id == Resource.Id.Nav_OddEvenResults)
{
currentTab = nameof(Resource.Id.Nav_OddEvenResults);
SetCurrentViewpager(PagerAdapterTypesEnum.OddEven);
CurrentTabLayout.GetTabAt(0).Select();
}
else if (id == Resource.Id.Nav_OddEvenDelays)
{
currentTab = nameof(Resource.Id.Nav_OddEvenDelays);
SetCurrentViewpager(PagerAdapterTypesEnum.OddEven);
CurrentTabLayout.GetTabAt(1).Select();
}
else if (id == Resource.Id.Nav_OddEvenNotifications)
{
currentTab = nameof(Resource.Id.Nav_OddEvenNotifications);
SetCurrentViewpager(PagerAdapterTypesEnum.OddEven);
CurrentTabLayout.GetTabAt(2).Select();
}
else if (id == Resource.Id.Nav_ColumnsStatistics)
{
currentTab = nameof(Resource.Id.Nav_ColumnsStatistics);
SetCurrentViewpager(PagerAdapterTypesEnum.Columns);
CurrentTabLayout.GetTabAt(0).Select();
}
else if (id == Resource.Id.Nav_ColumnsResultsPerNumbers)
{
currentTab = nameof(Resource.Id.Nav_ColumnsResultsPerNumbers);
SetCurrentViewpager(PagerAdapterTypesEnum.Columns);
CurrentTabLayout.GetTabAt(1).Select();
}
else if (id == Resource.Id.Nav_Settings)
{
currentTab = nameof(Resource.Id.Nav_Settings);
SetCurrentViewpager(PagerAdapterTypesEnum.Settings);
}
else if (id == Resource.Id.Nav_Info)
{
currentTab = nameof(Resource.Id.Nav_Info);
SetCurrentViewpager(PagerAdapterTypesEnum.Information);
}
else if (id == Resource.Id.Nav_Rate)
{
currentTab = nameof(Resource.Id.Nav_Rate);
RequestReview();
}
else if (id == Resource.Id.Nav_Share)
{
currentTab = nameof(Resource.Id.Nav_Share);
var appId = AppInfo.PackageName;
var url = $"https://play.google.com/store/apps/details?id={appId}";
ShareUri(url, GetString(Resource.String.app_slogan));
}
Microsoft.AppCenter.Analytics.Analytics.TrackEvent("SetCurrentView",
new Dictionary<string, string>()
{
{"SettingViewTo", currentTab},
}
);
drawer.CloseDrawer(GravityCompat.Start);
return true;
}
//The code that changes ViewPagers in order to change the views
public void SetCurrentViewpager(PagerAdapterTypesEnum personTypesEnum = PagerAdapterTypesEnum.OddEven)
{
switch (personTypesEnum)
{
case PagerAdapterTypesEnum.OddEven:
if (CurrentViewPagerAdapter is PagerAdapterOddEven)
return;
Title = GetString(Resource.String.app_name) + GetString(Resource.String.AppNameDash) +
GetString(Resource.String.Common_OddEven);
CurrentTabLayout.RemoveAllTabs();
CurrentTabLayout.Visibility = ViewStates.Visible;
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.OddEven_ResultsTabTitle)));
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.OddEven_DelaysTabTitle)));
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.OddEven_NotificationsTabTitle)));
CurrentTabLayout.TabGravity = TabLayout.GravityFill;
CurrentViewPagerAdapter = new PagerAdapterOddEven
(SupportFragmentManager, CurrentTabLayout.TabCount);
CurrentViewPager.Adapter = CurrentViewPagerAdapter;
CurrentViewPager.AddOnPageChangeListener(null);
break;
case PagerAdapterTypesEnum.Search:
if (CurrentViewPagerAdapter is PagerAdapterSearch)
return;
Title = GetString(Resource.String.app_name) + GetString(Resource.String.AppNameDash) +
GetString(Resource.String.Common_Draws);
CurrentTabLayout.RemoveAllTabs();
CurrentTabLayout.Visibility = ViewStates.Visible;
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.Search)));
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.NumbersSearch_BonusAndFrequency)));
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.NumbersPredictions_Title_WithoutTone)));
CurrentTabLayout.TabGravity = TabLayout.GravityFill;
CurrentViewPagerAdapter = new PagerAdapterSearch
(SupportFragmentManager, CurrentTabLayout.TabCount);
CurrentViewPager.Adapter = CurrentViewPagerAdapter;
CurrentViewPager.AddOnPageChangeListener(
new TabLayout.TabLayoutOnPageChangeListener(CurrentTabLayout));
CurrentTabLayout.TabSelected += (object sender, TabLayout.TabSelectedEventArgs e) =>
{
CurrentViewPager.SetCurrentItem(e.Tab.Position, true);
};
break;
case PagerAdapterTypesEnum.Columns:
if (CurrentViewPagerAdapter is PagerAdapterColumns)
return;
Title = GetString(Resource.String.app_name) + GetString(Resource.String.AppNameDash) +
GetString(Resource.String.Common_Columns);
CurrentTabLayout.RemoveAllTabs();
CurrentTabLayout.Visibility = ViewStates.Visible;
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.Columns_ColumnsStatistics)));
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.Columns_ColumnsResultsPerNumbers)));
CurrentTabLayout.TabGravity = TabLayout.GravityFill;
CurrentViewPagerAdapter = new PagerAdapterColumns
(SupportFragmentManager, CurrentTabLayout.TabCount);
CurrentViewPager.Adapter = CurrentViewPagerAdapter;
CurrentViewPager.AddOnPageChangeListener(
new TabLayout.TabLayoutOnPageChangeListener(CurrentTabLayout));
CurrentTabLayout.TabSelected += (object sender, TabLayout.TabSelectedEventArgs e) =>
{
CurrentViewPager.SetCurrentItem(e.Tab.Position, true);
};
break;
case PagerAdapterTypesEnum.Settings:
if (CurrentViewPagerAdapter is PagerAdapterSettings)
return;
Title = GetString(Resource.String.app_name) + GetString(Resource.String.AppNameDash) +
GetString(Resource.String.Common_OptionsTitle);
CurrentTabLayout.RemoveAllTabs();
CurrentTabLayout.Visibility = ViewStates.Gone;
CurrentTabLayout.AddTab(CurrentTabLayout.NewTab()
.SetText(GetString(Resource.String.Columns_ColumnsStatistics)));
CurrentViewPagerAdapter = new PagerAdapterSettings
(SupportFragmentManager, CurrentTabLayout.TabCount);
CurrentViewPager.Adapter = CurrentViewPagerAdapter;
CurrentViewPager.AddOnPageChangeListener(
new TabLayout.TabLayoutOnPageChangeListener(CurrentTabLayout));
CurrentTabLayout.TabSelected += (object sender, TabLayout.TabSelectedEventArgs e) =>
{
CurrentViewPager.SetCurrentItem(e.Tab.Position, true);
};
break;
case PagerAdapterTypesEnum.Information:
if (CurrentViewPagerAdapter is PagerAdapterInfo)
return;
Title = GetString(Resource.String.app_name) + GetString(Resource.String.AppNameDash) +
GetString(Resource.String.Common_Info);
CurrentTabLayout.RemoveAllTabs();
CurrentTabLayout.Visibility = ViewStates.Gone;
CurrentViewPagerAdapter = new PagerAdapterInfo
(SupportFragmentManager, 1);
CurrentViewPager.Adapter = CurrentViewPagerAdapter;
break;
}
}
Also for reference i will add the code of OnCreateView from the class that produce this crash although i dont think so the problem is there:
public class FragmentDrawsNumbersFrequency : AndroidX.Fragment.App.Fragment
{
...
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
if (inflater == null)
inflater = (LayoutInflater)Activity.GetSystemService(Context.LayoutInflaterService);
var view = inflater.Inflate(Resource.Layout.FragmentDrawsNumbersFrequency, container, false);
TimeToTextView = view.FindViewById<TextView>(Resource.Id.TimeToTextView);
TimeFromTextView = view.FindViewById<TextView>(Resource.Id.TimeFromTextView);
TimeFromButton = view.FindViewById<Button>(Resource.Id.TimeFromButton);
TimeToButton = view.FindViewById<Button>(Resource.Id.TimeToButton);
ListNameTextView = view.FindViewById<TextView>(Resource.Id.ListNameTextView);
DateToTextView = view.FindViewById<TextView>(Resource.Id.DateToTextView);
ShowInstructionsImageButton = view.FindViewById<ImageButton>(Resource.Id.ShowInstructionsImageButton);
ShowDateRangeUnlockImageButton = view.FindViewById<ImageButton>(Resource.Id.ShowDateRangeUnlockImageButton);
DateFromButton = view.FindViewById<Button>(Resource.Id.DateFromButton);
DateToButton = view.FindViewById<Button>(Resource.Id.DateToButton);
UnluckySpinner = view.FindViewById<Spinner>(Resource.Id.UnluckySpinner);
NumbersFrequencySpinner = view.FindViewById<Spinner>(Resource.Id.NumbersFrequencySpinner);
NumbersOrBonusSpinner = view.FindViewById<Spinner>(Resource.Id.NumbersOrBonusSpinner);
ClearHistoryButton = view.FindViewById<Button>(Resource.Id.ClearHistoryButton);
SearchMostLessViewsButton = view.FindViewById<Button>(Resource.Id.SearchMostLessViewsButton);
DailySearchSwitch = view.FindViewById<Switch>(Resource.Id.DailySearchSwitch);
SeperateCheckBox = view.FindViewById<CheckBox>(Resource.Id.SeperateCheckBox);
ShowListsCheckBox = view.FindViewById<CheckBox>(Resource.Id.ShowListsCheckBox);
TimeFromButton.Text = GetString(Resource.String.Common_DefaultFromTimePickerButton);
TimeToButton.Text = GetString(Resource.String.Common_DefaultToTimePickerButton);
RestoreHistoryList(Settings.DrawsNumbersFrequencyHistoryListViewKey);
HistoryListView = view.FindViewById<ListView>(Resource.Id.HistoryListView);
HistoryListViewAdapter = new HistoryListViewAdapter(Activity, HistoryList);
HistoryListView.Adapter = HistoryListViewAdapter;
HistoryListViewAdapter.NotifyDataSetChanged();
KinoUrl = GetString(Resource.String.KinoStats_KinoUrl_GR);
KinoParams = GetString(Resource.String.KinoStats_KinoParameters_GR);
ShowInstructionsImageButton.Click += (sender, e) => { ShowInstructions(); };
DailySearchSwitch.Click += (sender, e) =>
{
DisplayFunctions.SetDailySearch(DailySearchSwitch, DateToButton, DateToTextView,
ShowDateRangeUnlockImageButton, Settings.DailySearchesDrawsNumbersFrequencyKey);
};
DisplayFunctions.LoadDailySearch(DailySearchSwitch, DateToButton, DateToTextView,
ShowDateRangeUnlockImageButton, Settings.DailySearchesDrawsNumbersFrequencyKey);
PurchaseFunctions.EnableDateRange(DateToTextView, DateToButton, ShowDateRangeUnlockImageButton);
TimeFromButton.Click += (sender, e) => { ShowTimePicker(TimePickerEnum.From, FromDrawNumber, TimeFromButton); };
TimeToButton.Click += (sender, e) => { ShowTimePicker(TimePickerEnum.To, ToDrawNumber, TimeToButton); };
DateFromButton.Click += (sender, e) => { SelectDateFrom(DateFromButton); };
DateToButton.Click += (sender, e) => { SelectDateTo(DateToButton); };
ClearHistoryButton.Click += (sender, e) => { PromptClearHistory(); };
ShowDateRangeUnlockImageButton.Click += (sender, e) => { UnlockDateRange(); };
SearchMostLessViewsButton.Click += async (sender, e) =>
{
await ShowMostLessViews().ConfigureAwait(false);
};
FillKinoNums();
SetNumbersFrequencySpinner();
SetNumbersOrBonusSpinner();
//UnluckySpinner
SetUnluckySpinner();
return view;
}
Am i doing something heavy work here? should i change something or add a delay somewhere?
Thanks
Sign in to answer