Xamarin 选项卡导致 System.NullReferenceException
Xamarin tab causes System.NullReferenceException
我正在尝试在我的 Xamarin Android 应用程序中实现简单的选项卡界面。
我的 activity:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace SSMobile
{
[Activity (Label = "Tabs", MainLauncher = false, Icon = "@drawable/icon",Theme = "@android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
SetContentView (Resource.Layout.Main);
AddTab ("Panic", Resource.Drawable.tab_icon, new PanicFragment ());
AddTab ("Diagnostics", Resource.Drawable.tab_icon, new DiagnosticsFragment ());
AddTab ("Config", Resource.Drawable.tab_icon, new ConfigFragment ());
}
protected override void OnSaveInstanceState(Bundle outState)
{
outState.PutInt("tab", this.ActionBar.SelectedNavigationIndex);
base.OnSaveInstanceState(outState);
}
void AddTab (string tabText, int iconResourceId, Fragment view)
{
var tab = this.ActionBar.NewTab ();
tab.SetText (tabText);
tab.SetIcon (Resource.Drawable.tab_icon);
// must set event handler before adding tab
tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
{
var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
if (fragment != null)
e.FragmentTransaction.Remove(fragment);
e.FragmentTransaction.Add (Resource.Id.fragmentContainer, view);
};
tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e) {
e.FragmentTransaction.Remove(view);
};
this.ActionBar.AddTab (tab);
}
class PanicFragment: Fragment {
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.PanicFragment, container, false);
return view;
}
}
class DiagnosticsFragment: Fragment {
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.DiagnosticsFragment, container, false);
return view;
}
}
class ConfigFragment: Fragment {
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.ConfigFragment, container, false);
return view;
}
}
}
}
当我的应用打开 activity 时,出现错误:
System.NullReferenceException: Object reference not set to an instance
of an object
这一行:
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
异常日志:
System.NullReferenceException: Object reference not set to an instance of an object
at SSMobile.MainActivity.OnCreate (Android.OS.Bundle) [0x00009] in c:\Users\darek\Documents\Projects\SSMobile\SSMobile\MainActivity.cs:17
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1944
at at (wrapper dynamic-method) object.3a5a286c-d028-4989-a49e-efabfb6b0ae6 (intptr,intptr,intptr) <IL 0x00017, 0x0001f>
这段代码有什么问题?
我找到了无法使用 ActionBar 的原因。
在我的 activity 中,我使用了隐藏 TitleBar 的主题:
Theme = "@android:style/Theme.NoTitleBar"
这也使 ActionBar 隐藏且不可用。
所以,如果你想拥有 ActionBar,你就不能使用那个主题。如果您仍然需要隐藏标题栏,只需添加如下内容:
this.ActionBar.SetDisplayShowHomeEnabled (false);
this.ActionBar.SetDisplayShowTitleEnabled (false);
它将隐藏应用程序图标和标题。
希望它能为您节省几个小时的搜索时间:)
我正在尝试在我的 Xamarin Android 应用程序中实现简单的选项卡界面。 我的 activity:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace SSMobile
{
[Activity (Label = "Tabs", MainLauncher = false, Icon = "@drawable/icon",Theme = "@android:style/Theme.NoTitleBar")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
SetContentView (Resource.Layout.Main);
AddTab ("Panic", Resource.Drawable.tab_icon, new PanicFragment ());
AddTab ("Diagnostics", Resource.Drawable.tab_icon, new DiagnosticsFragment ());
AddTab ("Config", Resource.Drawable.tab_icon, new ConfigFragment ());
}
protected override void OnSaveInstanceState(Bundle outState)
{
outState.PutInt("tab", this.ActionBar.SelectedNavigationIndex);
base.OnSaveInstanceState(outState);
}
void AddTab (string tabText, int iconResourceId, Fragment view)
{
var tab = this.ActionBar.NewTab ();
tab.SetText (tabText);
tab.SetIcon (Resource.Drawable.tab_icon);
// must set event handler before adding tab
tab.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
{
var fragment = this.FragmentManager.FindFragmentById(Resource.Id.fragmentContainer);
if (fragment != null)
e.FragmentTransaction.Remove(fragment);
e.FragmentTransaction.Add (Resource.Id.fragmentContainer, view);
};
tab.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e) {
e.FragmentTransaction.Remove(view);
};
this.ActionBar.AddTab (tab);
}
class PanicFragment: Fragment {
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.PanicFragment, container, false);
return view;
}
}
class DiagnosticsFragment: Fragment {
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.DiagnosticsFragment, container, false);
return view;
}
}
class ConfigFragment: Fragment {
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);
var view = inflater.Inflate (Resource.Layout.ConfigFragment, container, false);
return view;
}
}
}
}
当我的应用打开 activity 时,出现错误:
System.NullReferenceException: Object reference not set to an instance of an object
这一行:
this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
异常日志:
System.NullReferenceException: Object reference not set to an instance of an object
at SSMobile.MainActivity.OnCreate (Android.OS.Bundle) [0x00009] in c:\Users\darek\Documents\Projects\SSMobile\SSMobile\MainActivity.cs:17
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1944
at at (wrapper dynamic-method) object.3a5a286c-d028-4989-a49e-efabfb6b0ae6 (intptr,intptr,intptr) <IL 0x00017, 0x0001f>
这段代码有什么问题?
我找到了无法使用 ActionBar 的原因。 在我的 activity 中,我使用了隐藏 TitleBar 的主题:
Theme = "@android:style/Theme.NoTitleBar"
这也使 ActionBar 隐藏且不可用。 所以,如果你想拥有 ActionBar,你就不能使用那个主题。如果您仍然需要隐藏标题栏,只需添加如下内容:
this.ActionBar.SetDisplayShowHomeEnabled (false);
this.ActionBar.SetDisplayShowTitleEnabled (false);
它将隐藏应用程序图标和标题。
希望它能为您节省几个小时的搜索时间:)