EditText 始终设置为 null
EditText always set to null
我不断收到“未设置对象实例的对象引用”。
这是我的代码:
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 编辑文本上时,它显示它为空,如下所示:
但不应该为null,我也找不到问题所在
更新:
所以事实证明我忘记在我的 OnCreate 方法中添加 SetContentView,但我仍然遇到同样的错误。我是否将 SetContentView 放在了正确的位置?
您上面的代码似乎在 OnCreate()
方法中缺少 SetContentView()
并在它之后调用 FindViewById
。
应该是这样的:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.xxxx); //this is the layout XML for your activity
EditText name2 = FindViewById<EditText>(Resource.Id.name2);
...
name2.Text = ""; // here the name2 will not be null;
}
我不断收到“未设置对象实例的对象引用”。
这是我的代码:
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 编辑文本上时,它显示它为空,如下所示:
但不应该为null,我也找不到问题所在
更新: 所以事实证明我忘记在我的 OnCreate 方法中添加 SetContentView,但我仍然遇到同样的错误。我是否将 SetContentView 放在了正确的位置?
您上面的代码似乎在 OnCreate()
方法中缺少 SetContentView()
并在它之后调用 FindViewById
。
应该是这样的:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.xxxx); //this is the layout XML for your activity
EditText name2 = FindViewById<EditText>(Resource.Id.name2);
...
name2.Text = ""; // here the name2 will not be null;
}