【问题标题】:Android Toast not working (App crashes with a NumberFormatException)Android Toast 不工作(应用程序因 NumberFormatException 而崩溃)
【发布时间】:2018-04-24 23:44:45
【问题描述】:

我不确定这是 IDE 问题还是我的编码问题。这个应用程序与我在网上关注的应用程序几乎相同,但我的应用程序不断崩溃。源代码链接:https://github.com/Gusri/AgeValidated。我的代码和这个差不多。

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="min.com.nyp.sit.myapplication.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Your name" />

<EditText
    android:id="@+id/editTextName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Age" />

<EditText
    android:id="@+id/editTextAge"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/buttonEnter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Validate"/>
</LinearLayout>

Kotlin 代码

   package min.com.nyp.sit.myapplication

   import android.support.v7.app.AppCompatActivity
   import android.os.Bundle
   import android.view.View
   import kotlinx.android.synthetic.main.activity_main.*
   import org.jetbrains.anko.toast

   class MainActivity : AppCompatActivity() {

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

    buttonEnter.setOnClickListener({

        val age:String = editTextAge.toString()
        val Name:String = editTextName.toString()

        if (age.toInt() <= 18){
           toast("Sorry $Name your Age not Register")
        }
        else{
           toast("Thank you $Name for Register")
        }

      })
    }
  }

Gradle 测试

buildscript {
ext.kotlin_version = '1.1.3-2'
ext.anko_version = '0.10.2'
repositories {
    google()
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.1.0-alpha03'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
 }

allprojects {
   repositories {
      google()
      jcenter()
   }
}

task clean(type: Delete) {
   delete rootProject.buildDir
}

Gradle 应用程序

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
defaultConfig {
    applicationId "min.com.nyp.sit.myapplication"
    minSdkVersion 26
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
       'proguard-rules.pro'
    }
   }
  }

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation 'com.android.support:appcompat-v7:26.1.0'
  implementation 'com.android.support.constraint:constraint-layout:1.0.2'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation('com.android.support.test.espresso:espresso-
  core:3.0.1', {
    exclude group: 'com.android.support', module: 'support-annotations'
  })
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "org.jetbrains.anko:anko-commons:0.10.2"
  }

最后但并非最不重要的一点是,我使用 anko 来展示吐司。因此修改了两个gradle脚本。

这是我的堆栈跟踪:

11-11 23:14:33.319 4580-4580/min.com.nyp.sit.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
 Process: min.com.nyp.sit.myapplication, PID: 4580
 java.lang.NumberFormatException: For input string: "android.support.v7.widget.AppCompatEditText{151ba00 VFED..CL. .F...... 0,290-1440,448 #7f070030 app:id/editTextAge}"
     at java.lang.Integer.parseInt(Integer.java:608)
     at java.lang.Integer.parseInt(Integer.java:643)
     at min.com.nyp.sit.myapplication.MainActivity$onCreate$1.onClick(MainActivity.kt:20)
     at android.view.View.performClick(View.java:6294)
     at android.view.View$PerformClick.run(View.java:24770)
     at android.os.Handler.handleCallback(Handler.java:790)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     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)

【问题讨论】:

  • 您可能需要共享崩溃的堆栈跟踪以获得调试帮助。
  • 问题不在于吐司。问题在这里:age.toInt()。检查你的 logcat。

标签: android android-studio android-gradle-plugin android-toast


【解决方案1】:

从 stact teace 它是一个 NumberFormat 异常,检查你在输入什么或将 EditText 限制为只有 xml 中的数字给出这样的属性

android:inputType="number"

【讨论】:

    【解决方案2】:

    您缺少从 findViewById 到 EditText 获得的视图的演员表

    var editTextName = findViewById(R.id.editTextName) as EditText
    var editTextAge= findViewById(R.id.editTextAge) as EditText
    

    然后,你要获取EditText的text属性

    val age:String = editTextAge.text.toString()
    val Name:String = editTextName.text.toString()
    
    if (age.toInt() <= 18){
           toast("Sorry $Name your Age not Register")
        }
    else{
           toast("Thank you $Name for Register")
        }
    

    确保 editTextAge 中的值代表一个有效的 int。

    【讨论】:

      【解决方案3】:

      只需使用这行代码:

      Toast.makeText(this@YourActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
      

      YourActivity 是您展示 toast 的地方。

      旧版 Android Toast

      Toast.makeText(YourActivity.this, "You clicked me.", Toast.LENGTH_SHORT).show()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-16
        • 1970-01-01
        • 1970-01-01
        • 2014-10-30
        • 1970-01-01
        • 1970-01-01
        • 2018-04-08
        • 1970-01-01
        相关资源
        最近更新 更多