0

La pestaña "Run" de Android Studio no me permite escribir por teclado al invocar la función "readLine()". Al ejecutar el siguiente código:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    var saludo = readLine()
    println("Saludo: $saludo")

}

Obtengo esta respuesta, dando la entrada como null sin permitirme escribir nada:

Launching 'app' on Nexus5NoSensor API 30.
$ adb shell am start -n "com.example.dam_04_primeros_pasos_kotlin_2/com.example.dam_04_primeros_pasos_kotlin_2.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 7856 on device 'Nexus5NoSensor_API_30 [emulator-5554]'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
W/_pasos_kotlin_: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
W/_pasos_kotlin_: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
I/System.out: Saludo: null
D/HostConnection: HostConnection::get() New Host Connection established 0xf46e2280, tid 7880
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
D/EGL_emulation: eglCreateContext: 0xf46e2ec0: maj 2 min 0 rcv 2
D/EGL_emulation: eglMakeCurrent: 0xf46e2ec0: ver 2 0 (tinfo 0xf4a2fbd0) (first time)
I/Gralloc4: mapper 4.x is not supported
D/HostConnection: createUnique: call
D/HostConnection: HostConnection::get() New Host Connection established 0xf46e40b0, tid 7880
D/goldfish-address-space: allocate: Ask for block of size 0x100
D/goldfish-address-space: allocate: ioctl allocate returned offset 0x3fbe51000 size 0x2000
D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 

Presupongo que debe haber un problema con el IDE.

Muchas gracias por su ayuda.

7
  • android studio no es para hacer applicaciones de consola.. el 27 oct. 2020 a las 14:54
  • Agrega la funcion readLine() ¿Como deseas obtener por teclado el valor?, en realidad esta es el problema ya que retorna un valor null, pero no podrías obtener nada por teclado.
    – Jorgesys
    el 27 oct. 2020 a las 14:59
  • Es cierto, @SinneroftheSystem, pero se me ha requerido en una práctica educativa. Si no, me liaría con layouts e InputNumbers para hacerlo, pero específicamente se pedía entrada por teclado en consola.
    – asxz
    el 27 oct. 2020 a las 15:32
  • @Elenasys, no sé si entiendo tu comentario. La práctica es más compleja que un simple var saludo = readLine(), pero como a resumidas cuentas el error es que no permite introducir por consola, pues he reducido el código para que la consulta sea más evidente.
    – asxz
    el 27 oct. 2020 a las 15:35
  • Ok , desde una app no puedes llamar readLine() , lo que tienes que hacer es poner por ejemplo un EditText y puedes lograr que lo que se escriba ahí se imprima en la consola.
    – Jorgesys
    el 27 oct. 2020 a las 15:37

1 respuesta 1

0

Sí es posible utilizar las funciones readLine() con Kotlin en Android Studio. Para ello, solo es necesario ir al directorio donde guardamos la clase MainActivity.kt, botón derecho, New > Kotlin File/Class > File. Una vez creado el archivo, dentro tendremos que insertar una funcion main:

fun main() {
   // Ejemplo de funcionamiento de readLine
   var saludo = readLine()!!
   println("Saludo: $saludo")
}

A partir de aquí, ya podremos codificar todo lo que deseemos para utilizar nuestro programa Kotlin por consola utilizando entrada por teclado. Es importante ejecutar dicha función a partir de la flecha verde que nos aparecerá a su izquierda. Si deseamos volver a ejecutar código de nuestra App, debemos recordar cambiar la configuración del Run de la barra de herramientas de Android Studio.

Tu Respuesta

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

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