我不确定这是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 Testing
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脚本被修改。
这是我的Stack跟踪:
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 :(得分:1)
从完整的teace开始,它是一个NumberFormat异常,检查你输入的内容或将EditText限制为只有xml中的数字给出这样的属性
android:inputType="number"
答案 1 :(得分:0)
您错过了从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。
答案 2 :(得分:0)
只需使用以下代码行:
Toast.makeText(this@YourActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
在这里,YourActivity就是您展示吐司的地方。
旧Android Toast :
Toast.makeText(YourActivity.this, "You clicked me.", Toast.LENGTH_SHORT).show()