Android Toast无法正常工作(应用程序崩溃)

我不确定这是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> 

科特林代码

  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) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

从stact teace这是一个NumberFormat异常,检查你给的输入或限制EditText只有在XML中的数字给这样一个属性

 android:inputType="number" 

你错过了从findViewById到EditText的视图转换

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

然后,你想获得EditText的文本属性

 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。