在switch语句中,我使用了一些在另一个类中定义的常量。
IDE告诉我constant expression required
。
我正在编写一个Android应用程序,在MainActivity中我这样写:
private static final int HOMEFRAGMENTINDEX = 0;
private static final int MESSAGEFRAGMENTINDEX = 1;
private static final int SNSFRAGMENTINDEX = 2;
private static final int DISCOVERYFRAGMENTINDEX = 3;
private static final int MINEFRAGMENTINDEX = 4;
public static int getHomefragmentindex() {
return HOMEFRAGMENTINDEX;
}
public static int getMessagefragmentindex() {
return MESSAGEFRAGMENTINDEX;
}
public static int getSnsfragmentindex() {
return SNSFRAGMENTINDEX;
}
public static int getDiscoveryfragmentindex() {
return DISCOVERYFRAGMENTINDEX;
}
public static int getMinefragmentindex() {
return MINEFRAGMENTINDEX;
}
在另一个类中我使用switch语句:
final int homeFragmentIndex = MainActivity.getHomefragmentindex();
final int messageFramentIndex = MainActivity.getMessagefragmentindex();
final int snsFragmentIndex = MainActivity.getSnsfragmentindex();
final int discoveryFragmentIndex = MainActivity.getDiscoveryfragmentindex();
final int mineFragentIndex = MainActivity.getMinefragmentindex();
switch (mCurIndicator) {
case homeFragmentIndex:
// do something
break;
case messageFramentIndex:
// do something
break;
case snsFragmentIndex:
// do something
break;
case discoveryFragmentIndex:
// do something
break;
case mineFragentIndex:
// do something
break;
default:
break;
}
Android Studio告诉我constant expression required
,但我确实使用了static fianl int
。也许你可以帮忙解决这个问题,谢谢。
答案 0 :(得分:0)
class Some{
public static final int HOMEFRAGMENTINDEX = 0;
public static final int MESSAGEFRAGMENTINDEX = 1;
public static final int SNSFRAGMENTINDEX = 2;
public static final int DISCOVERYFRAGMENTINDEX = 3;
public static final int MINEFRAGMENTINDEX = 4;
}
class AnotherClass{
void yourMethod(int mCurIndicator){
switch (mCurIndicator) {
case Some.HOMEFRAGMENTINDEX:
// do something
break;
case Some.MESSAGEFRAGMENTINDEX:
// do something
break;
case Some.SNSFRAGMENTINDEX:
// do something
break;
case Some.DISCOVERYFRAGMENTINDEX:
// do something
break;
case Some.MINEFRAGMENTINDEX:
// do something
break;
default:
break;
}
}
}