gpt4 book ai didi

c# - Xamarin 选项卡导致 System.NullReferenceException

转载 作者:太空宇宙 更新时间:2023-11-03 12:17:46 27 4
gpt4 key购买 nike

我正在尝试在我的 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 中,我使用了隐藏标题栏的主题:

Theme = "@android:style/Theme.NoTitleBar"

这也使得 ActionBar 隐藏且不可用。所以,如果你想拥有 ActionBar,你就不能使用那个主题。如果您仍然需要隐藏标题栏,只需添加如下内容:

this.ActionBar.SetDisplayShowHomeEnabled (false);
this.ActionBar.SetDisplayShowTitleEnabled (false);

它将隐藏应用程序图标和标题。

希望它能为您节省几个小时的搜索时间:)

关于c# - Xamarin 选项卡导致 System.NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28740183/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com