单击按钮后System.NullReferenceException:对象引用未设置为对象的实例

时间:2018-06-11 15:02:24

标签: android xamarin

using System;
using System.Collections.Generic;
using System.Drawing;
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 Gamemm1
{
    [Activity(Label = "ThirdActivity")]
    public class ThirdActivity : Activity
    {
        Button RannndomButton;
        TextView Information;
        TextView ResultInMillis;
        TextView ResultInMillisCount1;
        TextView EmptySpace;


        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.ThirdLayout);
            // Create your application here
            RannndomButton.FindViewById<Button>(Resource.Id.RannndomButton);
            Information.FindViewById<TextView>(Resource.Id.Information);
            ResultInMillis.FindViewById<TextView>(Resource.Id.ResultInMillis);
            ResultInMillisCount1.FindViewById<TextView>(Resource.Id.ResultInMillisCount1);
            EmptySpace.FindViewById<TextView>(Resource.Id.emptySpace);

            this.FindViewById<Button>(Resource.Id.RannndomButton).Click += RandomButtonIsClicked;



        }

        private void RandomButtonIsClicked(object sender, EventArgs e)
        {
            Information.Visibility = ViewStates.Invisible;
            ResultInMillis.Visibility = ViewStates.Invisible;
            ResultInMillisCount1.Visibility = ViewStates.Invisible;
            EmptySpace.Visibility = ViewStates.Invisible;

        }
    }
}

大家好,当我点击按钮时出现此异常:System.NullReferenceException:对象引用未设置为对象的实例。这里:RannndomButton.FindViewById(Resource.Id.RannndomButton); 。等待你的建议。其他3个按钮正在工作,我写的完全相同。我发布了我的大部分代码,因为我没有办法在代码中出错,所以问题可能就在其他地方。我试图创建额外的AXML布局,所以现在id甚至不保存。我无法通过FindViewById访问它们。问题是什么?

我在AXML中的代码是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/Information"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:visibility="visible"
        android:text="Click on  the button to start, it will spawn at random location make sure to click it as fast as you can to win 5times." />
    <TextView
        android:id="@+id/emptySpace"
        android:layout_height="30dip"
        android:layout_width="30dip"
        android:text="          "
        android:layout_below="@id/Information"
        android:visibility="visible"
        android:layout_alignParentLeft="true" />
    <TextView
        android:id="@+id/ResultInMillis"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Reaction time in milliseconds: "
        android:layout_below="@id/emptySpace"
        android:visibility="visible"
        android:layout_alignParentLeft="true" />
    <TextView
        android:id="@+id/ResultInMillisCount1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text=" Will be displayed here"
        android:layout_below="@id/emptySpace"
        android:visibility="visible"
        android:layout_toRightOf="@id/ResultInMillis" />
    <Button
        android:id="@+id/RannndomButton"
        android:layout_height="75dip"
        android:layout_width="75dip"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="#FFFFFF" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

也许更改用于“findViewById”的对象?

this.FindViewById<Button>(Resource.Id.RannndomButton);
this.FindViewById<TextView>(Resource.Id.Information);
this.FindViewById<TextView>(Resource.Id.ResultInMillis);
this.FindViewById<TextView>(Resource.Id.ResultInMillisCount1);
this.FindViewById<TextView>(Resource.Id.emptySpace);

答案 1 :(得分:0)

您实际上并未初始化视图对象。按如下方式更改OnCreate方法:

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.ThirdLayout);

    // Create your application here
    RannndomButton = this.FindViewById<Button>(Resource.Id.RannndomButton); 
    Information = this.FindViewById<TextView>(Resource.Id.Information); 
    ResultInMillis = this.FindViewById<TextView>(Resource.Id.ResultInMillis); 
    ResultInMillisCount1 = this.FindViewById<TextView>(Resource.Id.ResultInMillisCount1); 
    EmptySpace = this.FindViewById<TextView>(Resource.Id.emptySpace); 
    RannndomButton.Click += RandomButtonIsClicked; 
}