Android if语句未能检查条件
我有一个if语句不起作用,它不会检查它的条件,即使它是正确的,我不能理解这种行为。 请帮我找出错误。 这是我的代码:Android if语句未能检查条件,android,if-statement,switch-statement,Android,If Statement,Switch Statement,我有一个if语句不起作用,它不会检查它的条件,即使它是正确的,我不能理解这种行为。 请帮我找出错误。 这是我的代码: public Dialog onCreateDialog(int id) { AlertDialog.Builder alert = new AlertDialog.Builder(this); final SharedPreferences userPreferences = this.getSharedPreferences("strings", MOD
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;
}
要进行比较,应使用
yourString.equals(其他)
或yourString.equalsIgnoreCase(其他)
方法,而不是==。要进行比较,应使用yourString.equals(其他)
或yourString.equalsIgnoreCase(其他)
method而不是==。在Java中不能比较这样的字符串。您应该使用String.equals
(…)。对此的解释相当简单。=
操作符检查对象是否相同。这些字符串是具有相同值的不同对象。在Java中无法比较类似的字符串。您应该使用String.equals
(…)。对此的解释相当简单。=
操作符检查对象是否相同。那些字符串是具有相同值的不同对象。thk对于解释,我想知道为什么==不起作用,我在2-3个月前遇到了同样的问题。这是一种习惯,因为我来自解释人的C#backgroundthk,我想知道为什么==不起作用,我在2-3个月前遇到了同样的问题。我有点习惯了,因为我有C#的背景