【发布时间】:2015-10-28 03:45:15
【问题描述】:
在我的 Android 应用中:我将 R.id. 替换为 MyR.getResId()。
public void onClick(View v)
{
int id = v.getId();
switch (id)
{
case MyR.getResId(getApplicationContext(), "id", "title_left_image "):
exit();
break;
default :
break;
}
}
但是返回异常信息:
case 表达式必须是常量表达式
这是我的MyR代码:
public class MyR {
public static int getResId(Context context, String resType, String resName) {
int resId = 0;
String pck = context.getPackageName();
if (resId <= 0) {
resId = context.getResources().getIdentifier(resName, resType, pck);
if (resId <= 0)
resId = context.getResources().getIdentifier(
resName.toLowerCase(), resType, pck);
}
final int rid = resId;
return rid ;
}}
【问题讨论】:
标签: android expression constants