提问人:Xai 提问时间:12/13/2019 最后编辑:Xai 更新时间:12/13/2019 访问量:123
我想在 Xamarin 中制作一个增量游戏,并且我制作了一些计时器,这些计时器给了我一个异常错误
I want to make a incremental game in Xamarin, and i made some timers that give me an exception error
问:
我是初学者,我想为 android 制作一个带有橙色的答题器游戏,但我收到一个空引用异常错误,我搜索如何修复但仍然无法正常工作。这是我的第一个问题。
这是我的MainActivity.cs代码
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
private AutoOrange AutoOrangeScript;
public int oranges = 0;
int OPC = 1;
int upgradecost = 10;
int blendercost = 50;
public TextView orangestxt;
TextView opctext;
TextView opstext;
ImageButton orangebtn;
Button buyupgrade;
Button buyblender;
Timer Timerr;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_main);
orangestxt = FindViewById<TextView>(OrangeClicker.Resource.Id.orangetext);
opctext = FindViewById<TextView>(OrangeClicker.Resource.Id.opctxt);
opstext = FindViewById<TextView>(OrangeClicker.Resource.Id.opstxt);
orangebtn = FindViewById<ImageButton>(OrangeClicker.Resource.Id.orangebtn);
buyupgrade = FindViewById<Button>(OrangeClicker.Resource.Id.buyupgrade);
buyblender = FindViewById<Button>(OrangeClicker.Resource.Id.buyblender);
orangebtn.Click += Orange_Click;
buyupgrade.Click += BuyUpgrade_Click;
buyblender.Click += BuyBlender_Click;
AutoOrangeScript.AutoOrangeTimer.Interval = 1000;
AutoOrangeScript.AutoOrangeTimer.Elapsed += new ElapsedEventHandler(AutoOrangeScript.AutoOrangeTimer_Tick);
Timerr.Elapsed += new ElapsedEventHandler(Timer_Tick);
Timerr.Interval = 100;
Timerr.Enabled = true;
buyupgrade.Text = "Upgrade Orange - " + upgradecost + " Oranges +1 Per Click";
buyblender.Text = "Buy Blender - " + blendercost + " Oranges +1 Per Second";
opctext.Text = "Oranges per click: " + OPC;
opstext.Text = "Oranges per click: " + AutoOrangeScript.OPS;
}
void Orange_Click(object sender, System.EventArgs e)
{
oranges += OPC;
orangestxt.Text = oranges + " Oranges";
}
void Timer_Tick(object source, ElapsedEventArgs e)
{
orangestxt.Text = oranges + " Oranges";
buyupgrade.Text = "Upgrade Orange - " + upgradecost + " Oranges +1 Per Click";
buyblender.Text = "Buy Blender - " + blendercost + " Oranges +1 Per Second";
opctext.Text = "Oranges per click: " + OPC;
opstext.Text = "Oranges per click: " + AutoOrangeScript.OPS;
}
void BuyUpgrade_Click(object sender, System.EventArgs e)
{
if (oranges >= upgradecost)
{
oranges -= upgradecost;
if (OPC > 10)
{
upgradecost += 20;
} else {
upgradecost += 10;
}
OPC += 1;
opctext.Text = "Oranges per click: " + OPC;
buyupgrade.Text = "Upgrade Orange - " + upgradecost + " Oranges +1 Per Click";
}
}
void BuyBlender_Click(object sender, System.EventArgs e)
{
if (oranges >= blendercost)
{
oranges -= blendercost;
AutoOrangeScript.OPS += 1;
AutoOrangeScript.AutoOrangeTimer.Enabled = true;
if (AutoOrangeScript.OPS > 10)
{
upgradecost += 20;
} else
{
upgradecost += 10;
}
opstext.Text = "Oranges per second: " + AutoOrangeScript.OPS;
buyblender.Text = "Upgrade Blender - " + blendercost + " Oranges +1 Per Second";
}
}
}
这是我的第二个代码(AutoOrange.cs)
using System.Timers;
namespace OrangeClicker
{
public class AutoOrange
{
private MainActivity MainActivityScript;
public Timer AutoOrangeTimer;
public int OPS;
public void AutoOrangeTimer_Tick(object sender, System.EventArgs e)
{
MainActivityScript.oranges += OPS;
MainActivityScript.orangestxt.Text = MainActivityScript.oranges + "Oranges";
}
}
}
在这里我得到错误
AutoOrangeScript.AutoOrangeTimer.Interval = 1000; AutoOrangeScript.AutoOrangeTimer.Elapsed += new ElapsedEventHandler(AutoOrangeScript.AutoOrangeTimer_Tick);
Timerr.Elapsed += new ElapsedEventHandler(Timer_Tick); Timerr.Interval = 100; Timerr.Enabled = true;
答:
0赞
Ivan Ičin
12/13/2019
#1
您订阅其值为 null,因此它是 null 引用异常。在此之前创建。Timerr
Timer
评论