【发布时间】:2015-08-31 15:31:04
【问题描述】:
我正在尝试通过将 AlertDialog 的代码分离到另一个类中并根据代码中的要求调用该类来实现 AlertDialog。
这就是我的 AlertDialog 的方式
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace NAndroid
{
public class AlertViewController: Activity
{
public AlertViewController ()
{
}
public void ShowAlertBox (string titleTxt, string messageTxt)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle(titleTxt);
builder.SetMessage(messageTxt);
builder.SetCancelable(false);
builder.SetPositiveButton("OK", delegate { Finish(); });
RunOnUiThread (() => {
builder.Show();
} );
}
}
}
从其他类调用它
AlertViewController alertView = new AlertViewController ();
alertView.ShowAlertBox ("Test", "Test");
它在生产线上崩溃了。它抛出空指针异常
AlertDialog.Builder builder = new AlertDialog.Builder(this);
【问题讨论】:
-
您需要使用该活动的上下文来显示警报对话框
-
当我尝试使用 AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());它说“名称 getApplicationContext 在当前上下文中不存在。
标签: android xamarin android-alertdialog