我正在制作一个示例Android应用程序来测试事情,并且创建按钮处理程序时的错误如下,尽管我的构建成功了。
请帮忙
错误:未处理的异常: System.NullReferenceException:未将对象引用设置为对象的实例。发生
PLease帮助我找到合适的解决方案:
<style name="InputLayoutErrorHint" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/input_error_accent</item>
</style>
<style name="InputLayoutNormalHint" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/input_normal_accent</item>
</style>
<style name="InputLayoutAcceptedHint" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/input_accepted_accent</item>
</style>
我的Main.axml如下:
using Android.App;
using Android.Widget;
using Android.OS;
namespace Android_Picture
{
[Activity(Label = "Android Picture", MainLauncher = true, Icon =
"@drawable/icon")]
public class MainActivity : Activity
{
Button ButtonPrev;
Button ButtonNext;
TextView TextTitle;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
ButtonPrev = FindViewById<Button>(Resource.Id.buttonPrev);
ButtonNext = FindViewById<Button>(Resource.Id.buttonNext);
TextTitle = FindViewById<TextView>(Resource.Id.textTitle);
ButtonPrev.Click += ButtonPrev_Click; //error
ButtonNext.Click += ButtonNext_Click;
}
private void ButtonNext_Click(object sender, System.EventArgs e)
{
TextTitle.Text = "Next Clicked";
//throw new System.NotImplementedException();
}
private void ButtonPrev_Click(object sender, System.EventArgs e)
{
TextTitle.Text = "Previous Clicked";
//throw new System.NotImplementedException();
}
}
}
答案 0 :(得分:1)
该行:
SetContentView (Resource.Layout.Main);
非常重要,它实际上是为您的Activity设置布局。如果你没有这个FindViewById
将永远返回null。
要么膨胀正确的视图,要么创建一个。