【问题标题】:if statement fail to check the conditionif 语句无法检查条件
【发布时间】:2011-12-15 15:05:21
【问题描述】:

我有一个 if 语句不起作用,即使它是正确的,它也不会检查它的条件,我无法理解这种行为。 请帮我找出错误。 这是我的代码:

public Dialog onCreateDialog(int id) {
      AlertDialog.Builder alert = new AlertDialog.Builder(this);
     final SharedPreferences userPreferences = this.getSharedPreferences("strings", MODE_PRIVATE);
     final Editor edit = userPreferences.edit();

     switch (id) {
     case 1:
            alert.setTitle(lang_menu[3]);//"Gestione Preferiti"
         alert.setIcon(R.drawable.ic_menu_star);
         final String namesFav = userPreferences.getString("Name_Fav", "");
         final String locfav = userPreferences.getString("Loc_Fav", "");
         final String numfav = userPreferences.getString("Num_Fav", "");
         final String subnumfav = userPreferences.getString("SubNumFav", "");

        final String[] namesFavs = namesFav.split(";");
        final String[] locfavs= locfav.split(";");
        final String[] numfavs= numfav.split(";");
        final String[] subnumfavs = subnumfav.split(";");

         alert.setSingleChoiceItems(namesFavs, -1, new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int item) {
                pos = item;

            }
        });
        alert.setPositiveButton(lang_btn[2], new DialogInterface.OnClickListener() //"Visualizza"
        {
            public void onClick(DialogInterface dialog, int whichButton)
            {
              //if (SharedCode.isOnline(content)){
                 if (pos == -1){
                    Toast.makeText(content.getApplicationContext(), lang_error[2], Toast.LENGTH_LONG).show();//"Nessun preferito selezionato"
                 }else{
                    if (namesFavs[pos] == ""){
                        Toast.makeText(content.getApplicationContext(), lang_error[3], Toast.LENGTH_SHORT).show();//"Pagina inesistente"

                    }else{
                        prova= locfavs[pos];
                        SharedCode.loc = locfavs[pos].;
                        SharedCode.num = Short.parseShort(numfavs[pos]);
                        SharedCode.sub_num= Byte.parseByte(subnumfavs[pos]);
//-----------------------------------------------------------------------------------
                        if (SharedCode.loc == "aertel"){//this is the if that doesn't work
                            IrelandTeletext.getPage(content);
                        }
///////////////////////---------------------------------------------------------------
                    }


                }
                pos = -1;
            }
        });
break;
}
AlertDialog MyAlert  = alert.create();
    return MyAlert;
 }

【问题讨论】:

    标签: android if-statement switch-statement


    【解决方案1】:

    要比较Strings,您应该使用yourString.equals(other)yourString.equalsIgnoreCase(other) 方法而不是==。

    【讨论】:

      【解决方案2】:

      你不能在 Java 中比较这样的字符串。你应该使用String.equals(...)。这个解释很简单。 == 运算符检查对象是否相同。这些字符串是具有相同值的不同对象。

      【讨论】:

      • 感谢解释,我想知道为什么 == 不起作用
      • 我在 2-3 个月前遇到了同样的问题。这有点习惯,因为我来自 C# 背景
      猜你喜欢
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多