1

Estoy intentando crear un servicio para conectarme a la aplicación Kore. Para crear dicho servicio estuve mirando el código de android developer. Literalmente solo he seguido los pasos que me indican en la página. Aún así, al iniciar mi aplicación esta se detiene. Este es mi código:

package com.nerviosus.koreservice;

import android.app.Activity;

import android.content.Context;

import android.net.nsd.NsdManager;

import android.net.nsd.NsdServiceInfo;

import android.os.Bundle;

import java.net.ServerSocket;

public class MainActivity extends Activity  {

private final String SERVICE_TYPE = "_xbmc-jsonrpc-h._tcp.local.";
private String mServiceName = "NSD-Kore";
private NsdManager mNsdManager;
NsdManager.RegistrationListener mRegistrationListener;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    registerService(8080);
    initializeRegistrationListener();

}

public void registerService(int port) {
    NsdServiceInfo serviceInfo = new NsdServiceInfo();

    serviceInfo.setServiceName("KoreService");
    serviceInfo.setServiceType(SERVICE_TYPE);
    serviceInfo.setPort(port);
    mNsdManager = (NsdManager) getSystemService(Context.NSD_SERVICE);
    mNsdManager.registerService(
            serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);

}
public void initializeRegistrationListener() {
    mRegistrationListener = new NsdManager.RegistrationListener() {

        @Override
        public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
            // Save the service name. Android may have changed it in order to
            // resolve a conflict, so update the name you initially requested
            // with the name Android actually used.
            mServiceName = NsdServiceInfo.getServiceName();
        }

        @Override
        public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
            // Registration failed! Put debugging code here to determine why.
        }

        @Override
        public void onServiceUnregistered(NsdServiceInfo arg0) {
            // Service has been unregistered. This only happens when you call
            // NsdManager.unregisterService() and pass in this listener.
        }

        @Override
        public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
            // Unregistration failed. Put debugging code here to determine why.
        }
    };
}

Estos son los errores que aparecen en la consola:

2018-12-31 13:23:16.112 1463-1463/? E/adbd: failed to connect to socket 'localabstract:com.nerviosus.koreservice': Connection refused
2018-12-31 13:23:17.179 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:23:18.021 11085-11085/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.nerviosus.koreservice, PID: 11085
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nerviosus.koreservice/com.nerviosus.koreservice.MainActivity}: java.lang.IllegalArgumentException: listener cannot be null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.IllegalArgumentException: listener cannot be null
        at android.net.nsd.NsdManager.registerService(NsdManager.java:524)
        at com.nerviosus.koreservice.MainActivity.registerService(MainActivity.java:62)
        at com.nerviosus.koreservice.MainActivity.onCreate(MainActivity.java:49)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
2018-12-31 13:23:18.130 1748-3126/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2018-12-31 13:23:18.131 1748-3126/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
2018-12-31 13:23:19.143 1748-1923/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
2018-12-31 13:23:31.926 1748-1923/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
2018-12-31 13:23:33.127 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:23:44.081 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:23:44.085 1748-1768/? E/BatteryExternalStatsWorker: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2018-12-31 13:23:49.078 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:23:49.094 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:24:00.005 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:25:05.641 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:24:32.079 1748-1768/? E/BatteryExternalStatsWorker: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2018-12-31 13:25:36.083 1748-1768/? E/BatteryExternalStatsWorker: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2018-12-31 13:25:36.084 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:26:43.083 1748-1768/? E/BatteryExternalStatsWorker: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
2018-12-31 13:28:00.004 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 13:28:00.040 1748-1764/? E/memtrack: Couldn't load memtrack module
2018-12-31 19:17:07.904 11159-11159/com.google.android.calendar E/PrefServiceImpl: Primary account is null

Gracias en adelanto, y si puedo mejorar la formulación de la pregunta, agradezco que se mencione.

1 respuesta 1

2

El mensaje de error de desplegado en el LogCat indica el problema:

Caused by: java.lang.IllegalArgumentException: listener cannot be null

El listener no ha sido inicializado, el método que realiza la inicialización es initializeRegistrationListener() y el método donde es utilizado es registerService(), si revisas tu código en realidad estas realizando lo contrario:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    ...
    registerService(8080);
    initializeRegistrationListener();

}

este debería de ser el orden correcto, inicializar el listener mRegistrationListener y posteriormente utilizarlo:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    ...
    initializeRegistrationListener();
    registerService(8080);
}
6
  • De acuerdo, voy a probar aunque creo que ya lo intenté de esa forma.
    – nerviosus
    Commented el 1 ene. 2019 a las 19:10
  • Intentalo y revisa el LogCat, probablemente tienes otro problema.
    – Jorgesys
    Commented el 1 ene. 2019 a las 19:11
  • Si tengo otro problema en el LogCat, actualizo esta pregunta o creo una nueva?
    – nerviosus
    Commented el 1 ene. 2019 a las 19:12
  • 1
    No hace falta, todo funciona correctamente. Muchas gracias y feliz 2019!
    – nerviosus
    Commented el 1 ene. 2019 a las 19:14
  • 1
    Excelente Feliz año 2019 @nerviosus
    – Jorgesys
    Commented el 1 ene. 2019 a las 19:16

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.