【发布时间】:2020-12-17 13:55:52
【问题描述】:
我不断收到“未设置对象实例的对象引用”。
这是我的代码:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
payroll = JsonConvert.DeserializeObject<Payroll>(Intent.GetStringExtra("payroll"));
EditText name2 = FindViewById<EditText>(Resource.Id.name2);
EditText age2 = FindViewById<EditText>(Resource.Id.age2);
EditText finalPCB2 = FindViewById<EditText>(Resource.Id.finalPCB2);
EditText finalEPF2 = FindViewById<EditText>(Resource.Id.finalEPF2);
EditText finalSOCSO2 = FindViewById<EditText>(Resource.Id.finalSOCSO2);
EditText finalEIS2 = FindViewById<EditText>(Resource.Id.finalEIS2);
EditText grossSalary2 = FindViewById<EditText>(Resource.Id.grossSalary2);
EditText netSalary2 = FindViewById<EditText>(Resource.Id.netSalary2);
EditText employerEPF2 = FindViewById<EditText>(Resource.Id.employerEPF2);
EditText employerSOCSO2 = FindViewById<EditText>(Resource.Id.employerSOCSO2);
EditText employerEIS2 = FindViewById<EditText>(Resource.Id.employerEIS2);
name2.Text = " ";
age2.Text = " ";
finalPCB2.Text = " ";
finalEPF2.Text = " ";
finalSOCSO2.Text = " ";
finalEIS2.Text = " ";
grossSalary2.Text = " ";
netSalary2.Text = " ";
employerEPF2.Text = " ";
employerSOCSO2.Text = " ";
employerEIS2.Text = " ";
Button _reviewBack = FindViewById<Button>(Resource.Id.reviewBack);
_reviewBack.Click += PlayButton_Click;
_reviewBack.Click += (sender, e) => {
var payrollReview = new Intent(this, typeof(MainActivity));
StartActivity(payrollReview);
};
void PlayButton_Click(object sender, EventArgs e)
{
MediaPlayer _player = MediaPlayer.Create(this, Resource.Drawable.buttonclick);
_player.Start();
}
}
这是我的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/name2"
android:textSize="50px"
android:editable="false"
android:background="@null"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/age2"
android:textSize="50px"
android:editable="false"
android:background="@null"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false"
android:background="@null"
android:id="@+id/finalPCB2"
android:textSize="50px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/finalEPF2"
android:editable="false"
android:background="@null"
android:textSize="50px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/finalSOCSO2"
android:textSize="50px"
android:editable="false"
android:background="@null"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/finalEIS2"
android:editable="false"
android:background="@null"
android:textSize="50px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/grossSalary2"
android:textSize="50px"
android:editable="false"
android:background="@null"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/netSalary2"
android:textSize="50px"
android:editable="false"
android:background="@null"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Employer Details:"
android:textSize="63px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/employerEPF2"
android:editable="false"
android:background="@null"
android:textSize="50px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/employerSOCSO2"
android:editable="false"
android:background="@null"
android:textSize="50px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/employerEIS2"
android:background="@null"
android:textSize="50px"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/reviewBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back To Employees"
android:textSize="23dp"
android:backgroundTint="@android:color/holo_green_dark"
android:textColor="@android:color/white"/>
</LinearLayout>
当我将鼠标悬停在我的 name2 编辑文本上时,它显示它为空,如下所示:
但它不应该为空,我在这里找不到问题。
更新: 所以事实证明我忘记在我的 OnCreate 方法中添加 SetContentView,但我仍然遇到同样的错误。我是否将 SetContentView 放在正确的位置?
【问题讨论】:
-
我会检查您的任何变量是否不为空,您可能只是没有正确使用“findViewById”,或者此时您的所有视图都没有实例化。如果有些是空的,有些是填充的,那么你有一个比较点
-
@TheLemon 我所有的变量都是空的
-
有什么方法可以告诉天气视图已启动吗?
-
请显示你的xml
标签: c# xamarin.android android-edittext nullreferenceexception