使用字符串获取与该字符串同名的变量值

在android中(JAVA中也是一样的)用字符串获取某变量的值,则需要使用反射!

举个例子:

用名称获取资源图片

	public void setBmp(String name){
		int id = R.drawable.bg_normal;
		try {
			id = R.drawable.class.getField(name).getInt(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		bmpBg = BitmapFactory.decodeResource(res, id);
	}


你可能感兴趣的:(java,exception,android,String,null)