【问题标题】:EditText always set to nullEditText 始终设置为 null
【发布时间】: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


【解决方案1】:

您上面的代码似乎在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;
        
    }

【讨论】:

  • @dan51 你能更新你上面的onCreate代码吗?
  • 我重建了我的项目,它现在可以工作了!谢谢你:)
猜你喜欢
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多