不能使用“ int”或“ Integer”吗?

时间:2020-05-16 02:28:18

标签: android kotlin

如何在Android中将字符串转换为整数?我尝试查找示例,但是不能使用intparseIntIntegertoNumbernew(例如,它们不会作为选项填充)使用的代码,如果我继续尝试,它们会变成红色)。我不知道为什么,或者我做错了什么。我什至已经卸载/重新安装了Android Studio(从Canary转到Release Candidate)。

我有两个用随机整数(result&resultHist1)填充的textView,如果resultHist1不为空,我希望第三个textView(calcHist1)对它们求和。看来我最好的选择是如下所示,但我似乎无法使其正常工作。有什么想法吗?

        val a = result.toString().toInt()
        val b = resultHist1.toString().toInt()

        if (resultHist1.text.isNotBlank()) {
            calcHist1.text = (a+b).toString()
        } else {
            calcHist1.text = ""

当前,只要填充resultHist1,应用程序就会在我的AVD中崩溃,并且我的事件日志中也不会抛出异常。

如果需要更多详细信息,请告诉我。我决心解决这个问题!


Logcat更新:

2020-05-15 23:27:00.713 17953-17953/? I/mple.simpledic: Not late-enabling -Xcheck:jni (already on)
2020-05-15 23:27:00.721 17953-17953/? I/mple.simpledic: Unquickening 12 vdex files!
2020-05-15 23:27:00.722 17953-17953/? W/mple.simpledic: Unexpected CPU variant for X86 using defaults: x86
2020-05-15 23:27:00.766 17953-17953/com.example.simpledice I/mple.simpledic: The ClassLoaderContext is a special shared library.
2020-05-15 23:27:00.830 17953-17953/com.example.simpledice D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-15 23:27:00.830 17953-17953/com.example.simpledice D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-15 23:27:00.838 17953-17976/com.example.simpledice D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2020-05-15 23:27:00.840 17953-17976/com.example.simpledice D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2020-05-15 23:27:00.841 17953-17976/com.example.simpledice D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2020-05-15 23:27:00.917 17953-17953/com.example.simpledice W/mple.simpledic: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-05-15 23:27:00.917 17953-17953/com.example.simpledice W/mple.simpledic: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-05-15 23:27:01.018 17953-17974/com.example.simpledice D/HostConnection: HostConnection::get() New Host Connection established 0xf2392f10, tid 17974
2020-05-15 23:27:01.020 17953-17974/com.example.simpledice 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 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2020-05-15 23:27:01.022 17953-17974/com.example.simpledice W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-05-15 23:27:01.058 17953-17974/com.example.simpledice D/EGL_emulation: eglCreateContext: 0xf21b2970: maj 2 min 0 rcv 2
2020-05-15 23:27:01.090 17953-17974/com.example.simpledice D/EGL_emulation: eglMakeCurrent: 0xf21b2970: ver 2 0 (tinfo 0xf24f8810)
2020-05-15 23:27:01.117 17953-17974/com.example.simpledice I/Gralloc4: mapper 4.x is not supported
2020-05-15 23:27:01.118 17953-17974/com.example.simpledice D/HostConnection: createUnique: call
2020-05-15 23:27:01.118 17953-17974/com.example.simpledice D/HostConnection: HostConnection::get() New Host Connection established 0xf2392790, tid 17974
2020-05-15 23:27:01.119 17953-17974/com.example.simpledice D/eglCodecCommon: allocate: Ask for block of size 0x100
2020-05-15 23:27:01.136 17953-17974/com.example.simpledice D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3f9e13000 size 0x2000
2020-05-15 23:27:01.161 17953-17974/com.example.simpledice 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 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2020-05-15 23:27:01.175 17953-17974/com.example.simpledice D/EGL_emulation: eglMakeCurrent: 0xf21b2970: ver 2 0 (tinfo 0xf24f8810)
2020-05-15 23:27:07.408 17953-17953/com.example.simpledice D/AndroidRuntime: Shutting down VM
2020-05-15 23:27:07.409 17953-17953/com.example.simpledice E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.simpledice, PID: 17953
    java.lang.NumberFormatException: For input string: ""
        at java.lang.Integer.parseInt(Integer.java:627)
        at java.lang.Integer.parseInt(Integer.java:650)
        at com.example.simpledice.MainActivity$onCreate$1.onClick(MainActivity.kt:166)
        at android.view.View.performClick(View.java:7357)
        at android.view.View.performClickInternal(View.java:7334)
        at android.view.View.access$3600(View.java:808)
        at android.view.View$PerformClick.run(View.java:28200)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7478)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2020-05-15 23:27:07.430 17953-17953/com.example.simpledice I/Process: Sending signal. PID: 17953 SIG: 9

1 个答案:

答案 0 :(得分:0)

像这样从textview获取文本

 val a = result.text.toString()
 val b = resultHist1.text.toString()

 if(!a.isNullOrEmpty() &&  !b.isNullOrEmpty() ){
    val a = a.toInt()
    val b = b.toInt()
    calcHist1.text = (a+b).toString()
   }
   else
   {
   calcHist1.text = ""
   }
相关问题