-5

I am not sure whether it is an IDE issue or my coding issue. This app is almost the same as the one that i followed online but my app keeps crashing. The Link to the source code : https://github.com/Gusri/AgeValidated . My Codes are almost the same as this.

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 Codes

   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"
  }

Last but not least i used anko for the toast display. Thus the two gradle scripts are modified.

Here is my Stack trace:

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)
candied_orange
  • 7,036
  • 2
  • 28
  • 62

3 Answers3

1

from stact teace it's a NumberFormat exception , check what are you giving input or restrict the EditText to only numbers in the xml give an attribute like this

android:inputType="number"
himel
  • 500
  • 5
  • 14
0

You're missing a cast of the View you get from findViewById to EditText

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

Then, you want to get the text property of the 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")
    }

make sure that values inside editTextAge represent a valid int.

asif.ibtihaj
  • 361
  • 5
  • 14
0

just use this line of code:

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

Here YourActivity is where you are showing your toast.

Old Android Toast:

Toast.makeText(YourActivity.this, "You clicked me.", Toast.LENGTH_SHORT).show()
Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87