在早期版本的android中找不到方法java.lang.String.isEmpty

在早期版本的android中找不到方法java.lang.String.isEmpty,java,android,string,backwards-compatibility,Java,Android,String,Backwards Compatibility,我正在为Android编写一个应用程序,所有的应用程序在我的Android 4.1上都可以正常工作,但是当我在另一个Android 2.2上测试它时,除了表单,所有的应用程序都可以正常工作,当你点击按钮发送表单时,应用程序崩溃 我试着对不同的代码进行注释,我在if-else中发现了问题,但我不知道为什么我希望你能帮我解决这个问题 此代码发送以下表单: private void createAccount(){ EditText nombre = (EditText) findVi

我正在为Android编写一个应用程序,所有的应用程序在我的Android 4.1上都可以正常工作,但是当我在另一个Android 2.2上测试它时,除了表单,所有的应用程序都可以正常工作,当你点击按钮发送表单时,应用程序崩溃

我试着对不同的代码进行注释,我在if-else中发现了问题,但我不知道为什么我希望你能帮我解决这个问题

此代码发送以下表单:

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输出为

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)

引发异常的行是
if(name.isEmpty()==false….

根据Android文档,
.isEmpty()
直到API级别9才添加。这是您的问题。Android 2.2是API级别8


文档:

这很有趣,因为我自己昨天就碰到了这个问题

罪魁祸首不是
if…else
本身,而是
name.isEmpty()
等等

LogCat应该给你一个提示——对我来说,它记录了一些评论,说“找不到virtual java.lang.String.isEmpty…”

如果您在Eclipse中将鼠标悬停在
isEmpty()
上,它会诚实地告诉您,
isEmpty()
是在…API级别9(2.3)中引入的

如果希望代码在Froyo中运行,则需要使用
name.length()>0
——这将在API级别1开始时起作用


当然,您可以在清单中设置minSDKLevel=9,如果可以接受的话,您可以忽略与2.2及以下版本的兼容性。

您可以发布堆栈跟踪吗?顺便说一句,不管是谁否决了这个问题,我认为这个问题并没有那么糟糕。虽然可以通过仔细查看logcat输出找到答案,但它说明了两个重要的经验教训:(1)不仅Android特定的类,甚至java.lang的类(如本例中的java.lang.String)都是版本兼容性的主题(2)尽管在清单中指定了SDK级别,编译器并没有警告使用不兼容的调用。对我来说,这两个类都是令人不快的惊喜:(@amoiseyev在SO上转储代码并说“它崩溃了”或“它不起作用"根据网站指南,这绝对不是一个好问题。有同样问题的人不太可能找到这个问题及其答案。另一方面,发布日志并询问具体错误将是一个有用的问题。好吧,足够公平,让我试着解决这个问题……这应该是一个评论,我不能评论。除非我遗漏了什么。我认为我的声誉还不够好。现在我的声誉不够好,因为你投了反对票。我的计划是编辑我的答案。很抱歉。我不知道评论的声誉。没关系。谢谢你取消了投反对票。我希望他们能删除它,因为我想离开一个社区的时候很多t和不能,但留下答案也是不合适的。@pasta12你只需要再多投一张赞成票就可以发表评论了——差一点了!