我想自定义警报对话框标题背景,应用程序崩溃而不显示警报对话框。(自定义视图只填充消息区域,不包括标题面板和按钮面板。我想自定义默认的标题面板和按钮面板,这里以标题为例。)
public static class MyAlertDialog
{
private static AlertDialog _alertDialog;
public static void Show(Context context)
{
var factory = LayoutInflater.From(context);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context)
.SetTitle("myTitle")
.SetView(factory.Inflate(Resource.Layout.DialogRegister, null))
.SetCancelable(true);
_alertDialog = alertDialogBuilder.Create();
var titleView = (TextView)_alertDialog.FindViewById(Android.Resource.Id.Title); //get title view from the Android resource not from my custom view
//titleView.SetBackgroundResource(Resource.Color.PrimaryColor);
titleView.SetBackgroundColor(Android.Graphics.Color.Red);
_alertDialog.Show();
}
}
我从主要活动中调用对话框:
[Activity(Label = "My Activity", MainLauncher = true)]
public class HomeActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.home);
Button btnMyButton = FindViewById<Button>(Resource.Id.MyButton);
btnMyButton.Click += (object sender, EventArgs e) =>
{
MyAlertDialog.Show(this);
};
......
}
}
VS 抛出运行时异常并要求我中断或继续。我点击继续。然后应用程序崩溃。日志:
03-09 11:10:47.057 E/mono ( 1185): [0x4001f730:] EXCEPTION handling: Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
03-09 11:10:49.956 I/Email ( 451): ReconcilePopImapAccountsSync: start
03-09 11:10:50.276 I/Email ( 451): ReconcilePopImapAccountsSync: done
03-09 11:11:07.417 E/mono ( 1185): [0x4001f730:] EXCEPTION handling: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
03-09 11:11:07.727 E/mono ( 1185): [0x4001f730:] EXCEPTION handling: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
03-09 11:11:16.846 F/ ( 1185): * Assertion: should not be reached at /Users/builder/data/lanes/monodroid-lion-bigsplash/0e0e51f9/source/mono/mono/mini/debugger-agent.c:5980
03-09 11:11:16.846 I/mono ( 1185): Stacktrace:
03-09 11:11:16.846 I/mono ( 1185):
03-09 11:11:16.866 E/mono ( 1185): [0x2a118c70:] EXCEPTION handling: System.NullReferenceException: Object reference not set to an instance of an object
03-09 11:11:16.906 E/mono ( 1185):
03-09 11:11:16.906 E/mono ( 1185): Unhandled Exception:
03-09 11:11:16.906 E/mono ( 1185): System.NullReferenceException: Object reference not set to an instance of an object
03-09 11:11:16.937 I/mono ( 1185): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
我在模拟器中使用 Android 4.1.2 armeabi-v7a 对其进行了测试。(该项目将支持架构 armeabi、armeabi-v7a 和 x86。)
谢谢你的帮助。(我知道我可以将带有消息的标题放在自定义内容视图中。但我也想自定义它的其他部分。所以我只是以标题为例。)
我注意到虽然我 SetTitle("myTitle"),但 titleView.text 是一个空字符串。我在获取默认标题视图时错了吗
var titleView = (TextView)_alertDialog.FindViewById(Android.Resource.Id.Title);
同样,我的自定义视图不包含标题视图。