-3

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....

amoiseyev
  • 327
  • 2
  • 7
  • Can you post the stack trace? – resueman Jul 11 '13 at 16:28
  • 1
    BTW, whoever down-voted the question, I do not think question is that bad. While answer could be found by careful looking at logcat output, it illustrates two important lessons learned: (1) not only Android-specific classes, but even classes of java.lang (like java.lang.String in this case) are subject of version compatibility (2) despite specifying SDK level in manifest, compiler does not warn about using incompatible calls. For me both were unpleasant surprises :( – amoiseyev Jul 11 '13 at 16:51
  • @amoiseyev dumping code on SO and saying "it crashes" or "it doesn't work" is simply *not* a good question, ever, as per the site guidelines. It is unlikely anyone with this same problem would ever be able to find this Q and its answer. Posting the logcat and asking about *the specific error* on the other hand, would be a useful question. – Brian Roach Jul 11 '13 at 17:06
  • Ok, fair enough, let me try to fix the question then... – amoiseyev Jul 11 '13 at 17:33

2 Answers2

5

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.

amoiseyev
  • 327
  • 2
  • 7
2

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

Ben Kane
  • 9,331
  • 6
  • 36
  • 58
  • 2
    this should be a comment – sanbhat Jul 11 '13 at 16:31
  • I can't comment. Unless I'm missing something. I don't think I have enough reputation yet. Now I have less because you downvoted.. My plan is to edit my answer. – Ben Kane Jul 11 '13 at 16:33
  • sorry about that.. I wasn't aware of reputation for comment.. – sanbhat Jul 11 '13 at 16:34
  • It's okay. Thanks for undoing the downvote. I wish they would remove that because there are a lot of times when I want to leave a comment and can't, but it would also be inappropriate to leave an answer. – Ben Kane Jul 11 '13 at 16:35
  • @pasta12 you just need to get one more upvote to be able to comment - so close! – amoiseyev Jul 11 '13 at 17:04