Question

I'm programming an app for Android, and all the app works fine on my Android 4.1, but when I'm testing it on my other Android 2.2 all works fine except the form, when you click the button to send the form the app crashes.

I'd tried commenting different of code and I've found the problem in the if-else, but I don't know why I hope you can help me with this.

This code sends the form:

private void createAccount(){
        EditText nombre = (EditText) findViewById(R.id.name);
        EditText mail = (EditText) findViewById(R.id.mail);
        EditText tel = (EditText) findViewById(R.id.phone);
        EditText pass = (EditText) findViewById(R.id.pass);
        EditText pass2 = (EditText) findViewById(R.id.pass2);

    name = nombre.getText().toString();
    email = mail.getText().toString();
    phone = tel.getText().toString();
    pas = pass.getText().toString();
    pas2 = pass2.getText().toString();

    if(name.isEmpty() == false && email.isEmpty() == false && phone.isEmpty() == false && pas.isEmpty() == false && pas2.isEmpty() == false){
        if(pas.equals(pas2)){
            respTxt = Cloud.CreateAccout(phone, name, email, pas, tel_id, tel_oper, tel_country);

            TextView alert = (TextView)findViewById(R.id.reg_alert);

            ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
            if(networkInfo != null && networkInfo.isConnected()){
                if(respTxt.equals("email")){
                    alert.setText(getText(R.string.reg_error1));
                    regBtn.setEnabled(true);
                }else if(respTxt.equals("error") || respTxt.equals("errorc")){
                    alert.setText(getText(R.string.reg_error2));
                    regBtn.setEnabled(true);
                }else if(respTxt.equals("correct")){
                    SharedPreferences settings = getSharedPreferences(prefs_file, 0);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putBoolean("AccountCreated", true);
                    editor.putString("status", "ok");
                    editor.commit();
                    Intent move = new Intent(AppRegis.this, HomeAct.class);
                    startActivityForResult(move, 0);
                }
            }else{
                alert.setText(getText(R.string.noInternet));
                regBtn.setEnabled(true);
            }

        }else{
            TextView alert = (TextView)findViewById(R.id.reg_alert);
            alert.setText(getText(R.string.reg_alert)+" "+email);
            regBtn.setEnabled(true);
        }
    }else{
        TextView alert = (TextView)findViewById(R.id.reg_alert);
        alert.setText(getText(R.string.reg_alert2).toString());
        regBtn.setEnabled(true);
    }
}

LogCat output is

07-11 17:21:26.251: I/dalvikvm(335): Could not find method java.lang.String.isEmpty, referenced from method com.example/testactivity.createAccount()
07-11 17:21:26.251: W/dalvikvm(335): VFY: unable to resolve virtual method 177: Ljava/lang/String;.isEmpty ()Z
07-11 17:21:26.251: D/dalvikvm(335): VFY: replacing opcode 0x6e at 0x0042
07-11 17:21:26.251: D/dalvikvm(335): VFY: dead code 0x0045-0069 in Lcom/example/testactivity/createAccount() (Landroid/content/Context;Lcom/example/testtctivity;I)V
07-11 17:21:26.361: D/AndroidRuntime(335): Shutting down VM
07-11 17:21:26.361: W/dalvikvm(335): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-11 17:21:26.371: E/AndroidRuntime(335): FATAL EXCEPTION: main
07-11 17:21:26.371: E/AndroidRuntime(335): java.lang.NoSuchMethodError: java.lang.String.isEmpty
07-11 17:21:26.371: E/AndroidRuntime(335):  at com.example.testactivity.createAccount()(testactivity.java:178)

Line where exception is thrown is if(name.isEmpty() == false....

Was it helpful?

Solution

This is funny, because I have bumped into this issue literally yesterday myself...

Culprit is not if...else itself, but name.isEmpty() and alike.

LogCat should have given you a hint - for me it have logged comments saying "can not find virtual java.lang.String.isEmpty.....".

And if you hover over isEmpty() in Eclipse, it will honestly tell you that isEmpty() is introduced in... API level 9, which is 2.3.

If you want your code to run in Froyo, you need to use name.length() > 0 - this will work starting API level 1.

Of course, you can set minSDKLevel = 9 in manifest and forget about compatibility with 2.2 and below, if it is acceptable.

OTHER TIPS

As per Android Documentation, .isEmpty() was not added until API Level 9. That's your issuse. Android 2.2 is API Level 8.

Documentation: http://developer.android.com/reference/java/lang/String.html#isEmpty%28%29

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top