不能使用 "int "或 "Integer"?

问题描述 投票:0回答:1

如何在Android中把字符串变成整数? 我试着找了一些例子,但我无法使用 "int "或 "Integer"。int, parseInt, Integer, toNumbernew 比如说(它们并没有作为要使用的代码选项填充,如果我试图继续前进,它们就会变成红色)。我不知道为什么,也不知道自己做错了什么。我甚至卸载了Android Studio(从Canary到Release Candidate)。

我有两个用随机整数填充的文本视图(result & resultHist1),我希望第三个文本视图(calcHist1)在resultHist1不是空白的情况下将它们相加。 看来我最好的选择是像下面这样的,但我似乎不能使它工作。 有什么好办法吗?

        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
java android-studio kotlin int integer
1个回答
0
投票

从文本视图中获取文本,像这样

 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 = ""
   }
© www.soinside.com 2019 - 2024. All rights reserved.