C# Xamarin Android NullReferenceException

C# Xamarin Android NullReferenceException,c#,xamarin.android,C#,Xamarin.android,我现在在活动之间传递数据时遇到问题。在看了很多教程之后,我似乎不明白为什么会出现系统空引用异常。这是第一个活动的代码。它允许用户输入一些文本。然后,按下按钮后,应将字符串传递到下一个活动: using Android.App; using Android.Widget; using Android.OS; using Android.Content; namespace DiagnosticProjFive { [Activity(Label = "DiagnosticProjFive

我现在在活动之间传递数据时遇到问题。在看了很多教程之后,我似乎不明白为什么会出现系统空引用异常。这是第一个活动的代码。它允许用户输入一些文本。然后,按下按钮后,应将字符串传递到下一个活动:

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;

namespace DiagnosticProjFive
{
    [Activity(Label = "DiagnosticProjFive", MainLauncher = true)]
    public class MainActivity : Activity
    {
        Button button;
        EditText editText;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            button = FindViewById<Button>(Resource.Id.toNextScreen);
            editText = FindViewById<EditText>(Resource.Id.enterWords);

            button.Click += delegate
            {
                var enteredStrTxt = editText.Text;
                string enteredStr = enteredStrTxt.ToString();
                Intent intent = new Intent(this, typeof(screenTwo));
                intent.PutExtra("Extra", enteredStr);
                this.StartActivity(intent);

            };
        }
    }
}
使用Android.App;
使用Android.Widget;
使用Android.OS;
使用Android.Content;
名称空间诊断Proj5
{
[活动(Label=“DiagnosticProjFive”,MainLauncher=true)]
公共课活动:活动
{
按钮;
编辑文本编辑文本;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
button=FindViewById(Resource.Id.toNextScreen);
editText=findviewbyd(Resource.Id.enterWords);
按钮。单击+=委派
{
var enteredstrxt=editText.Text;
字符串enteredStr=enteredstrxt.ToString();
意向意向=新意向(此,类型为(屏幕二));
意图。额外输入(“额外”,输入str);
这一点。触觉(意图);
};
}
}
}
下一个活动。它应该从上一个活动中获取文本,并将TextView设置为输入的文本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace DiagnosticProjFive
{
    [Activity(Label = "screenTwo")]
    public class screenTwo : Activity
    {
        TextView label;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here
            label = FindViewById<TextView>(Resource.Id.textView1);
            label.Text = Intent.GetStringExtra("Extra");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
名称空间诊断Proj5
{
[活动(Label=“screenTwo”)]
公共课第二幕:活动
{
文本视图标签;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//在此处创建应用程序
label=findviewbyd(Resource.Id.textView1);
label.Text=Intent.GetStringExtra(“额外”);
}
}
}
Main.axml的XML源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/enterWords" />
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toNextScreen" />
</LinearLayout>

screenTwo.axml的XML源:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1" />
</LinearLayout>

我似乎不明白为什么会出现系统空引用异常

您没有为
屏幕二设置布局,这就是系统为空的原因
引用异常

解决方案: 使用
SetContentView()
方法为
screenTwo
活动添加布局:

 protected override void OnCreate(Bundle savedInstanceState)
 {
      base.OnCreate(savedInstanceState);

      SetContentView(Resource.Layout.screenTwo); // <-- Add this line

      label = FindViewById<TextView>(Resource.Id.textView1);
      label.Text = Intent.GetStringExtra("Extra");
 }
protectedoverride void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

SetContentView(Resource.Layout.screenTwo);//您可以尝试调试并查看
Null引用的出现位置吗?是的,它出现在label.Text=Intent.GetStringExtra(“Extra”);行。第一个屏幕工作正常,但当我按下按钮转到下一个活动时,屏幕变黑,异常显示。未处理的异常:System.NullReferenceException:Object reference未设置为对象的实例。这是因为标签为
null
。您确定有
TextView
>在
screenActivity
xml中调用textView1?如果您有疑问,请发布xml。只需发布xml@NovicePolymath,快乐编码:)