【问题标题】:Android case context.getResources().getIdentifier(resName, resType, pck):Android案例context.getResources().getIdentifier(resName, resType, pck):
【发布时间】: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


    【解决方案1】:

    试试这个,

    public void onClick(View v)
    {
        int id = v.getId();
        if (id == MyR.getResId(getApplicationContext(), "id", "title_left_image ")){
               exit();
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多