if或else if中的语句从不执行Android
我有一个基于参数返回位图对象的方法。 if条件没有执行,我不知道到底是什么问题,我正在尝试在if条件中打印Log语句,但它不起作用if或else if中的语句从不执行Android,android,Android,我有一个基于参数返回位图对象的方法。 if条件没有执行,我不知道到底是什么问题,我正在尝试在if条件中打印Log语句,但它不起作用 public static Bitmap getWesternZodiacIcon(Context context,String westernZodiacSign)throws Exception{ //Context context = null; Bitmap westernZodiacIcon = null;
public static Bitmap getWesternZodiacIcon(Context context,String westernZodiacSign)throws Exception{
//Context context = null;
Bitmap westernZodiacIcon = null;
if (westernZodiacSign.equals("Aries")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_aries);
Log.v("westernZodiacIcon", "westernZodiacSign");
}
else if (westernZodiacSign.equals("Taurus")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_taurus);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Gemini")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_gemini);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Cancer")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_cancer);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Leo")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_lion);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Virgo")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_virgin);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Libra")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_scales);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Scorpio")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_scorpion);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Sagittarius")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_sagittarius);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Capricorn")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_capricor);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Aquarius")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_aquarius);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else if (westernZodiacSign.equals("Pisces")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_pisces);
Log.v("westernZodiacIcon", westernZodiacSign);
}
return westernZodiacIcon;
}
你必须考虑所有的情况。添加其他内容:
...
else if (westernZodiacSign.equals("Pisces")) {
westernZodiacIcon=BitmapFactory.decodeResource(context.getResources(),R.drawable.zodiac_pisces);
Log.v("westernZodiacIcon", westernZodiacSign);
}
else {
Log.v("westernZodiacIcon", "The null hypothesis for astrology is...");
}
另外,可能会按照方法签名中的指示引发异常,因此请尝试捕获它并记录日志。计算机不使用magic,westernZodiacSign可能不等于任何这些内容,或者您的方法从未被调用。你有没有试过放一个
Log.v(“westernZodiacSign”,westernZodiacSign)
在if测试之前?检查字符大小写,如果您不确定是否可以使用ignoreCase或先更改为小写,然后检查是否使用debugger,您可能还想用equalsIgnoreCase替换equals。是的,它可以工作,如果我将Log语句放在if条件之前,那么我可以在westernZodiacSign中获取值,但是在IF或ELSE IF中,它不起作用。因为上下文是null
,所以代码抛出一个NullPointerException
。您似乎忽略了调用代码中的异常。这解释了为什么日志语句无法访问。