0

I have a button which works according to its function when user enters a number in an EditText. However,when my EditText is numerically empty,when i click on the same button, it make my app stop working. Any help?

Please consider the "numerically" part because i could not find any answers to my query?

code


  public void send(View v){
  String text = edittext.getText().toString();
  if (text.matches("")) {
 myButton.setEnabled(false);
 return;
       }
 }

xml

<LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
        android:layout_width="0dp"
        android:layout_weight="1"
        android:inputType="number"
        android:layout_height="wrap_content"
        android:hint="Enter:"
        android:id="@+id/edittext"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        android:onClick="send"
        android:id="@+id/btn"/>
</LinearLayout>

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

logcat

01-16 10:49:15.208 3014-3014/? I/SELinux: Function:    selinux_android_load_priority [0], There is no sepolicy file.

01-16 10:49:15.208 3014-3014/? I/SELinux: Function: selinux_android_load_priority [1], There is no sepolicy version file.

01-16 10:49:15.208 3014-3014/? I/SELinux: Function: selinux_android_load_priority , priority version is VE=GOOGLE_POLICY


01-16 10:49:15.208 3014-3014/? I/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
01-16 10:49:15.208 3014-3014/? E/SELinux: [DEBUG] seapp_context_lookup: seinfoCategory = default
01-16 10:49:15.208 3014-3014/? E/SELinux: [DEBUG] seapp_context_lookup: seinfoCategory = default
01-16 10:49:15.208 3014-3014/? D/dalvikvm: Late-enabling CheckJNI
01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 254:   Landroid/app/Notification$Builder;.setLocalOnly   (Z)Landroid/app/Notification$Builder;
01-16 10:49:15.298 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x00c8
01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzj
01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 549:   Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
 01-16 10:49:15.308 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
 01-16 10:49:15.358 3014-3037/com.example.dell.uomopenday I/GMPM: App measurement is starting up
 01-16 10:49:15.358 3014-3037/com.example.dell.uomopenday E/GMPM: getGoogleAppId failed with status: 10
 01-16 10:49:15.368 3014-3037/com.example.dell.uomopenday E/GMPM: Uploading is not possible. App measurement disabled
 01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
 01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
 01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve interface method 19469: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
 01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
 01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
 01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve interface method 19473: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
 01-16 10:49:15.378 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
 01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 627: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday W/dalvikvm: VFY: unable to resolve virtual method 649: Landroid/content/res/TypedArray;.getType (I)I
01-16 10:49:15.408 3014-3014/com.example.dell.uomopenday D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
01-16 10:49:15.508 3014-3014/com.example.dell.uomopenday I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
                                                                   OpenGL ES  Shader Compiler Version: E031.24.00.08
                                                                   Build Date: 03/21/14 Fri
                                                                   Local Branch: AU200+patches_03212014
                                                                   Remote Branch: 
                                                                   Local   Patches: 
                                                                          Reconstruct Branch: 
01-16 10:49:15.758 3014-3014/com.example.dell.uomopenday D/OpenGLRenderer: Enabling debug mode 0

buttononclick

 button.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {

    Search = new ArrayList<>();

    int points = Integer.parseInt(edittext.getText().toString());
    for (int i = 0; i < courses.length; i++) {

        Course c2 = courses[i];

        if (c2.getPoints() <= points) {
            Search.add(c2);
        }

    }

    Course searching [] = new Course[Search.size()];
    searching = Search.toArray(searching);

    adapter = new listAdapter(MainActivity.this, searching);
    myList.setAdapter(adapter);

}

 String text = edittext.getText().toString();
 if (text.matches("")) {
 button.setEnabled(false);
 return;
   }
 });
Mary Jane
  • 41
  • 1
  • 6
  • have you tried `android:inputType="number"` – Iamat8 Jan 16 '16 at 05:37
  • i edited my above question but still my app crashes. any solution? – Mary Jane Jan 16 '16 at 06:33
  • i have no logcat errors. my app is launching correcting in my mobile but on clicking the button, it just crashes. – Mary Jane Jan 16 '16 at 06:37
  • it does show any problem with your `edittext` perhaps it show with `Notification` – Iamat8 Jan 16 '16 at 07:03
  • Yes... You said that,on clicking of the button the app crashes...than,please post your code of onclicklistener. try debugging... – Aditi Parikh Jan 17 '16 at 13:01
  • @AditiParikh: i posted it plz help – Mary Jane Jan 18 '16 at 03:54
  • Hi..Your code seems ok..but try debugging.. May be it is throwing null pointer exception..Please cross check,you have all the variables declared properly. Try with debugging and find on which line you are getting the error. To stop the force close,you should have written the code in try-catch block. – Aditi Parikh Jan 18 '16 at 04:50

2 Answers2

0

First step, limit the input type to numbers.

In the layout:

android:inputType="number"

Or in code:

<EditText>.setInputType(InputType.TYPE_CLASS_NUMBER);

Then to validate the text is a number:

final String value = <EditText>.getText();
if (!StringUtils.isNullOrWhiteSpace(value) && value.matches("\\d*")) {
    // The value is a number! Yay!
} else {
    // The value is NOT a number! Boo!
}

Edit: For completeness here is my StringUtils (not necessary):

public class StringUtils {
    public static final String EMPTY = "";

    public static boolean isNullOrWhiteSpace(String value) {
        if (value == null) {
            return true;
        }

        for (int i = 0; i < value.length(); i++) {
            if (!Character.isWhitespace(value.charAt(i))) {
                return false;
            }
        }

        return true;
    }
}
Cory Charlton
  • 8,868
  • 4
  • 48
  • 68
-1

If you only want to check that your EditText is empty or not, for your Button functionality you can do this like given below:

Button myButton
EditText edittext = (EditText) findViewById(R.id.edittext);

String text = edittext.getText().toString();
if (text.matches("")) {
   myButton.setEnabled(false);
    return;
}

By the way I don't understand what do you mean by numerically empty? But if you only want to enter numeric data in you EditText you can add below given functionality in your EditText in XML.

android:inputType="number"
Devendra Dagur
  • 840
  • 1
  • 14
  • 35
  • i edited my above question but still my app crashes. any solution? – Mary Jane Jan 16 '16 at 06:33
  • @MaryJane What error you are getting, You can post copy and paste your logcat error by edit your question or you can just tell me here in comment section. – Devendra Dagur Jan 16 '16 at 06:35
  • i have no logcat errors. my app is launching correcting n my app but on clicking the button, it just crashes. – Mary Jane Jan 16 '16 at 06:37
  • @MaryJane Yes I understand, your app crashes when you click on button, but there must be some some error, that's why your app crashes, to know you logcat error, click you android monitor window in below you android studio, and when that window open , see under logcat tab, you will find error , which are responsible for you app crash , errors will come after crashing of your app. – Devendra Dagur Jan 16 '16 at 06:41
  • @MaryJane Your logcat clearly saying that, `android.app.Notification$Builder` method not found, it means you miss some thing like You not added support liberary `android-support-v4.jar` or may be you not import it correctly `import android.support.v4.app.NotificationCompat;` or possible you are not using notification builder correctly, To know how use correctly notification builder go to this link http://stackoverflow.com/questions/6391870/how-exactly-to-use-notification-builder and you should post your whole class code. – Devendra Dagur Jan 16 '16 at 07:06
  • i posted my buttononclicklistener part yet it crashes. i have checked your above comment and added. still same problem. please help me! – Mary Jane Jan 18 '16 at 03:55
  • @MaryJane Everything looks good with your code..........ok ......do one thing .........try to debugging ........in your android studio make breakpoints on every method and suspicious code.....then run your code in debug mode .....and look where it giving you error. ?? if you don't know how to debug then learn it from internet. By debugging you at least able find where your code giving you error. – Devendra Dagur Jan 18 '16 at 06:30