如何从C#统一代码从服务中的类调用方法

时间:2019-04-04 09:49:21

标签: java c# unity3d

我的问题是,当我统一从c#调用Java代码时,我得到了空引用,但我的类是100%不为空。我有个问题,就是我的服务正在另一个进程上运行,但是我没有找到有关调用Java方法的任何信息,这不会扩展class中的unityplayeractivity。

我试图获取我的服务实例并获取所需的类来调用方法,但是它不起作用。我也尝试从我的服务中调用方法。我得到了所有3个调试的统一性,然后抛出了空引用。现在我的代码如下:

if (condition)
    {
        Debug.Log("1111111111111111111111111111111111111111111111111");
        AndroidJavaClass javaClass = new AndroidJavaClass("com.fileControllers.FileTransferService");
        Debug.Log("22222222222222222222222222222222222222222222222222");
        AndroidJavaObject controller = javaClass .GetStatic<AndroidJavaObject>("serverContactsMessagesController");
        Debug.Log("33333333333333333333333333333333333333333333333333");
        controller.Call("UnregisterRadio", id, cai);
    }

我的FIleTransfer类如下:

package com.fileControllers;
public class FileTransferService extends Service  {
public static SIPMessageController serverContactsMessagesController;
public static FileTransferService Instance;

    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.d("TestManualDisconnect", "Starting file transfer service");
        Instance = this;
        serverContactsMessagesController = new SIPMessageController();
    }
}

我的SIPMessageController类:

package com.fileControllers.SIPMessage;
public SIPMessageController()
{
    Start();
}
// Use this for initialization
void Start () {
    serverTextNotifications = new ServerMessagesHandler(FileTransferService.mContext);
    client = new UDPListener(m_port);
    client.start();
    listener = new SIPMessageListener();
    client.setMessageRecivedListener(listener);
    responseController = new SIPResponseController();
    //loginController = new ServerContactsMessagesController();
    Log.d("TestSIP", "Started sip messages controller");
}

取消注册方法:

public void UnregisterRadio(int serverID, int CAI)
{
    Log.d("TESTTESTTEST", "UNREGISTERING RADIO");
    try {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    SIPRegistrationMessage message = new SIPRegistrationMessage(ExtensionTypes.Register);
    message.userName = "unregister";
    SIPHeader header = new SIPHeader(false, true, true, false, true);
    int reqNumber = new Random().nextInt(1000000);
    stream.write(header.Render());
    ConvertSIPData.WriteVarInt(stream, reqNumber);
    stream.write(message.Render());
    byte[] messageBytes = stream.toByteArray();
    DatagramPacket packet = new DatagramPacket(messageBytes, messageBytes.length, GetAccessuarByRadioID(serverID, CAI), m_port);
    client.Send(packet);
    }
    catch (IOException e)
    {
        Log.d("Errors", "Error on Sending SIP message " + e.getMessage());
    }
}

不要认为该错误有帮助,但是:

E/Unity: NullReferenceException: Object reference not set to an instance of an object
  at UIChatMessages.ConnectToServer () [0x000bb] in <c21560bc798f49c3b4e266f4b3a67dd1>:0 
  at UnityEngine.Events.InvokableCall.Invoke () [0x00011] in <b6d510486de34305839d30de90e040c0>:0 
  at UnityEngine.Events.UnityEvent.Invoke () [0x00023] in <b6d510486de34305839d30de90e040c0>:0 
  at UnityEngine.UI.Button.Press () [0x00027] in <64e28a9e74f14b82b76e6bb05ae2a678>:0 
  at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00011] in <64e28a9e74f14b82b76e6bb05ae2a678>:0 
  at UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00008] in <64e28a9e74f14b82b76e6bb05ae2a678>:0 
  at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00070] in

有什么解决方案吗?现在它的活动与广播方法到我的服务,但我想从我的C#代码中调用服务方法

0 个答案:

没有答案