【发布时间】:2021-12-04 07:36:41
【问题描述】:
我的应用程序发送和接收 TCP 字符串。其中一个字符串是根据字符串使对象可见。我当前的代码给了System.NullReferenceException has been thrown Object reference not set to an instance of an object.
我找不到正确的访问方法,我猜是因为 RunOnUI 似乎没有访问此权限。
我当前的代码:
监听类:(全类的片段,如果需要我可以发布更多)。
public void ReadCallback(IAsyncResult ar)
{
MainActivity ma = new MainActivity();
String content = string.Empty;
StateObject so = (StateObject)ar.AsyncState;
Socket handle = so.workSocket;
int bytesRead = handle.EndReceive(ar);
if (bytesRead > 0)
{
so.sb.Append(System.Text.Encoding.ASCII.GetString(so.buffer, 0, bytesRead));
content = so.sb.ToString();
}
if (content.IndexOf("0") > -1)
{
ma.RunOnUiThread(() =>
{
//This is where the received data is passed
ma.SortData(content);
});
Send(handle, content);
}
else
{
handle.BeginReceive(so.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), so);
}
}
以及 SortData 的代码:
public void SortData(string data)
{
if(data == "0")
{
txtmsg.Visibility = ViewStates.Invisible;
}
} //This is the only code that's called, the txtmsg.Visibility part is the error producer once called.
【问题讨论】:
标签: c# multithreading xamarin xamarin.android