• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Cant understand what I am doing wrong

demeteor

Lurker
Dec 4, 2015
2
0
Hello everyone, I am new tho this forums and also new to android developing. what I am trying to accomplish at the moment is to check for validation on the form (if user added name lastname and age) then check if the user added correct age older than 4 younger than 80 (example numbers) . I made a small code about it but I am sure i am making some mistakes since whenever i press the button it crushes here is the entire button code


Code:
  public void onSaveClick(View v) {
        //check for valitation for Text
        EditText name = (EditText) findViewById(R.id.nametxt);
        EditText lname = (EditText) findViewById(R.id.lnametxt);
        EditText age = (EditText) findViewById(R.id.agetxt);

        String nameischecked = name.getText().toString();
        String lnameischecked = lname.getText().toString();
        String ageischecked = age.getText().toString();
        final String checkage = age.getText().toString();
        int value = Integer.parseInt(checkage);
        Toast toast = null ;
        if (nameischecked.matches("")) {
            if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE ) {
            toast = Toast.makeText(getApplicationContext(), "You Must Enter A Name !! ", Toast.LENGTH_SHORT);
            toast.show();
            toast = null;
            return;
            }

        }
        else if(lnameischecked.matches("")) {
            if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE) {
                toast = toast.makeText(getApplicationContext(),"You Must Enter A Lanename !! " , Toast.LENGTH_SHORT);
                toast.show();
                toast = null;
                return;
            }
        }
        else if(ageischecked.matches("")) {
            if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE) {
                toast = toast.makeText(getApplicationContext(),"You Must Enter An Age !! " , Toast.LENGTH_SHORT);
                toast.show();
                toast = null;
                return;
            }
        }
        else if(4 >= value || value >= 80) {
            if (toast == null || toast.getView().getWindowVisibility() != View.VISIBLE) {
                toast = toast.makeText(getApplicationContext(), "The Ages Must be from 4 to 80  ", Toast.LENGTH_SHORT);
                toast.show();
                toast = null;
                return;
            }
        }
         else {
                 Button savebtn = (Button) findViewById(R.id.button);
                 Button editbtn = (Button) findViewById(R.id.button2);
                Button servbtn = (Button) findViewById(R.id.button3);
                 savebtn.setEnabled(false);
                 name.setEnabled(false);
                 lname.setEnabled(false);
                age.setEnabled(false);
            }
    }
 
Can you post your Logcat output please? This will show the exception which caused your to crash.
12-04 16:41:58.273 2159-2159/com.example.chara.otherme I/art: Not late-enabling -Xcheck:jni (already on)
12-04 16:41:58.273 2159-2159/com.example.chara.otherme I/art: Late-enabling JIT
12-04 16:41:58.292 2159-2159/com.example.chara.otherme I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
12-04 16:41:58.340 2159-2159/com.example.chara.otherme W/System: ClassLoader referenced unknown path: /data/app/com.example.chara.otherme-2/lib/x86
12-04 16:41:58.435 2159-2175/com.example.chara.otherme D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-04 16:41:58.443 2159-2171/com.example.chara.otherme I/art: Background sticky concurrent mark sweep GC freed 8283(524KB) AllocSpace objects, 0(0B) LOS objects, 30% free, 2MB/3MB, paused 7.551ms total 25.665ms
12-04 16:41:58.443 2159-2159/com.example.chara.otherme D/: HostConnection::get() New Host Connection established 0xac03d280, tid 2159
12-04 16:41:58.510 2159-2175/com.example.chara.otherme I/OpenGLRenderer: Initialized EGL, version 1.4
12-04 16:41:58.553 2159-2175/com.example.chara.otherme W/EGL_emulation: eglSurfaceAttrib not implemented
12-04 16:41:58.553 2159-2175/com.example.chara.otherme W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad76cbc0, error=EGL_SUCCESS
12-04 16:42:01.641 2159-2159/com.example.chara.otherme D/AndroidRuntime: Shutting down VM
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: FATAL EXCEPTION: main
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: Process: com.example.chara.otherme, PID: 2159
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method for android:eek:nClick
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:278)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.view.View.performClick(View.java:5198)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21147)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.view.View.performClick(View.java:5198)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21147)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: Caused by: java.lang.NumberFormatException: Invalid int: ""
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.Integer.invalidInt(Integer.java:138)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:358)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.Integer.parseInt(Integer.java:334)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at com.example.chara.otherme.MainActivity.onSaveClick(MainActivity.java:48)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.support.v7.internal.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.view.View.performClick(View.java:5198)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21147)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-04 16:42:03.513 2159-2171/com.example.chara.otherme I/art: WaitForGcToComplete blocked for 5.533ms for cause HeapTrim
12-04 16:42:03.843 2159-2159/com.example.chara.otherme I/Process: Sending signal. PID: 2159 SIG: 9
 
Upvote 0
Your problem is caused by a NumberFormatException

Code:
12-04 16:42:01.641 2159-2159/com.example.chara.otherme E/AndroidRuntime: Caused by: java.lang.NumberFormatException: Invalid int: ""

I suspect you typed nothing in the age textfield. Your code should be validating each field to make sure it's not null or "".
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones