【发布时间】:2015-05-05 14:08:53
【问题描述】:
我正在制作一个 Android 游戏,有代码可以将高分信息发送到我的 Web 的 PHP 代码。
奇怪的是,这在 unity-editor 中运行良好,但在我制作 .apk 并在真正的 android 手机上运行后,它不起作用并出现错误。
错误如下(Eclipse logcat 已知)
05-05 22:56:47.997: I/Unity(20561): NullReferenceException: 对象 引用未设置为对象 05-05 22:56:47.997 的实例: I/Unity(20561):在 HighScore.GetHash (System.String usedString) [0x00000] in :0 05-05 22:56:47.997: I/Unity(20561): 在 HighScore+c__Iterator14.MoveNext () [0x00000] 中 :0
代码是,首次运行高分时注册昵称的功能。
public IEnumerator RegisterName(string name)
{
string finalResult = "";
string tables = "";
for (int m = 0; m < difficultyModes.Length; m++)
{//Create a list of tables to send
if (m < difficultyModes.Length - 1)
{
tables += difficultyModes[m].databaseName + " ";
}
else
{
tables += difficultyModes[m].databaseName;
}
}
WWWForm rsFm = new WWWForm();
rsFm.AddField("name", name);
rsFm.AddField("tables", tables);
rsFm.AddField("hash", GetHash(name));
WWW rs = new WWW(registerUserURL, rsFm);
yield return rs;
Debug.Log(rs.text + " : " + difficultyModes[modeIndex].displayName);
finalResult = rs.text;
if (finalResult.Equals("Already Used"))
{
runningHsServer = 0;
status = "Name Already Used";
TitleScript.Instance.PopupMsgWithNoBack("Name is already taken.", true);
yield break;
}
else if (finalResult.Equals("Registration Complete"))
{//We Registered Now Update Score
PlayerPrefs.SetInt("nameRegistered", 1);
PlayerPrefs.SetString("registeredName", name);
TitleScript.Instance.PopupMsgWithNoBack("Name registered!", true);
TitleScript.Instance.PopupNo();
NewUI.Instance.myNick.text = name;
myNickName = name;
NewUI.Instance.OpenHigh();
}
else
{
runningHsServer = 0;
status = finalResult; yield break;
}
}
和 GetHash 部分。 (这里会出现NullReference错误)
string GetHash(string usedString)
{ //Create a Hash to send to server
MD5 md5 = MD5.Create();
//byte[] bytes = Encoding.ASCII.GetBytes(usedString+secretKey); // this wrong because can't receive korean character
byte[] bytes = Encoding.UTF8.GetBytes(usedString + secretKey);
byte[] hash = md5.ComputeHash(bytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("x2"));
}
return sb.ToString();
}
【问题讨论】:
-
您是否尝试过在
GetHash方法中添加空检查?