Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Toast不工作(应用程序因NumberFormatException崩溃)_Android_Android Studio_Android Gradle Plugin_Android Toast - Fatal编程技术网

Android Toast不工作(应用程序因NumberFormatException崩溃)

Android Toast不工作(应用程序因NumberFormatException崩溃),android,android-studio,android-gradle-plugin,android-toast,Android,Android Studio,Android Gradle Plugin,Android Toast,我不确定这是IDE问题还是我的编码问题。这个应用程序和我在网上使用的几乎一样,但我的应用程序一直崩溃。指向源代码的链接:。我的代码与此几乎相同 XML Gradle应用程序 apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 26 buildToolsVersio

我不确定这是IDE问题还是我的编码问题。这个应用程序和我在网上使用的几乎一样,但我的应用程序一直崩溃。指向源代码的链接:。我的代码与此几乎相同

XML

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)

从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的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")
    }
val-age:String=editTextAge.text.toString()
val Name:String=editTextName.text.toString()

如果(age.toInt()只需使用这行代码:

Toast.makeText(this@YourActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
在这里,您的活动是您展示您的祝酒词的地方

古老的Android Toast

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

您可能需要共享来自崩溃的堆栈跟踪,以便获得调试帮助。问题不是toast。问题是:age.toInt()。请检查您的日志。
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")
    }
Toast.makeText(this@YourActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
Toast.makeText(YourActivity.this, "You clicked me.", Toast.LENGTH_SHORT).show()