0

我通过 xamarin 为 android 编写了小应用程序。它通过 LAN 向 Windows 发送一些数据。安装后我尝试打开应用程序,但出现错误:

代码应用:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
 using Android.OS;
 using System.Net.Sockets;
 using System.Net;
 using System.IO;
 using System.ComponentModel;
using System.Threading;
using System.Text;

namespace Android4programm
{
 [Activity(Label = "Controller", MainLauncher = true, Icon =       "@drawable/icon")]
public class MainActivity : Activity
{
    public EditText editTextMessage;
    public Button buttonConnect;
    public Button buttonDisconnect;
    public Button buttonSend;
    TcpClient client;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        buttonConnect = FindViewById<Button>(Resource.Id.button_Connect);
        buttonDisconnect = FindViewById<Button>(Resource.Id.button_Disconnect);
        buttonSend = FindViewById<Button>(Resource.Id.button_Send);
        //editTextMessage = FindViewById<EditText>(Resource.Id.edittext_message);
        editTextMessage = FindViewById<EditText>(Resource.Id.edittext_Message);

        buttonConnect.Click += Connect;
        buttonDisconnect.Click += Disconnect;
        buttonSend.Click += Send;
        client = new TcpClient();

        buttonDisconnect.Enabled = false;
        buttonSend.Enabled = false;
        editTextMessage.Enabled = false;
    }  
    private void Connect(object sender, EventArgs e)
    {
        IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse("192.168.1.1"), 12345);
        client.Connect(IP_End);
        buttonConnect.Text = "Connected";
        buttonConnect.Enabled = false;
        buttonDisconnect.Enabled = true;
        buttonSend.Enabled = true;
        editTextMessage.Enabled = true;
    }
    private void Disconnect(object sender, EventArgs e)
    {
        string msg = "CancelConnection";
        byte[] buff = Encoding.ASCII.GetBytes(msg);
        client.GetStream().Write(buff, 0, buff.Length);
        client.Close();
    }
    private void Send(object sender, EventArgs e)
    {
        string msg;
        if (editTextMessage.Text!=null)
        {
            msg = editTextMessage.Text;
        }
        else
        {
            msg = "Deafult message";
        }

        byte[] buff = Encoding.ASCII.GetBytes(msg);
        client.GetStream().Write(buff, 0, buff.Length);
    }
}
}

搜索论坛:

  • 我重新安装了应用
  • 我结清了现金
  • 我添加了所有权限
  • 我的版本 android 2.3.5 有效,程序需要 2.3 API 10 lvl
4

0 回答 0