android toast 出错,Android Toast无法正常工作(App崩溃)(示例代码)
我不确定它是IDE问题还是我的编码问题。这个应用程序几乎与我在网上关注的应用程序相同,但我的应用程序一直在崩溃。链接到源代码:https://github.com/Gusri/AgeValidated。我的代码几乎与此相同。XMLxmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.androi
我不确定它是IDE问题还是我的编码问题。这个应用程序几乎与我在网上关注的应用程序相同,但我的应用程序一直在崩溃。链接到源代码:https://github.com/Gusri/AgeValidated。我的代码几乎与此相同。
XML
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">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your name" />
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age" />
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:id="@+id/buttonEnter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Validate"/>
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 App
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)
更多推荐
所有评论(0)