我有一个WifiScanning类:
public class WifiScanning extends AbstractSetting {
/**
*
*/
private static final long serialVersionUID = 226897434530036069L;
public WifiScanning(Object valueToApply) {
super(valueToApply, WifiScanning.class);
}
/**
* For persistence only
*/
public WifiScanning() {
super(null, WifiScanning.class);
}
如您所见,它有两个构造函数。一个是我的简单持久层,它是一个空的构造函数,因此newInstance()可以工作,另一个参数是由我的应用程序定义的标准参数。其他代码假设必须有一个带有单个参数的构造函数,否则它会抛出异常。
/**
*
* @param setting
* @param ctx
* @return
* @throws SettingException
*/
private synchronized static AbstractSetting getOriginalSetting(AbstractSetting setting,
Context ctx) throws SettingException {
Class<? extends AbstractSetting> clazz = setting.getClass();
try {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
for (Constructor<?> c : constructors) {
if(c.getParameterTypes().length == 1) {
Object original = setting.getCurrentSettingValue(ctx);
LOG.debug("Caching original value '"+original+"' for "+clazz.getSimpleName());
return (AbstractSetting) c.newInstance(original);
}
}
/*
* ###################### DEBUG BLOCK ######################
*
* This has been put here to work out why we are getting to this point in the code when
* using WifiScanning.java
*/
LOG.error("There are "+constructors.length+" constructors for "+clazz.getName()+" which we got from "+setting);
for (Constructor<?> c : constructors) {
if(c.getParameterTypes().length == 1) {
LOG.debug("Found the consructor! How the hell can that be?");
}
else {
LOG.error("Unusable constructor: "+c.toGenericString());
LOG.error("From: "+c.getDeclaringClass());
LOG.error("Modifiers:");
LOG.error("private="+Modifier.isPrivate(c.getModifiers()));
LOG.error("protected="+Modifier.isProtected(c.getModifiers()));
LOG.error("public="+Modifier.isPublic(c.getModifiers()));
LOG.error("static="+Modifier.isStatic(c.getModifiers()));
Type[] genericParameterTypes = c.getGenericParameterTypes();
LOG.error("Constructor has "+genericParameterTypes.length+" generic parameter types");
for (Type type : genericParameterTypes) {
LOG.error("Generic parameter type: "+type.getClass().getName());
}
Class<?>[] parameterTypes = c.getParameterTypes();
LOG.error("Constructor has "+parameterTypes.length+" parameters");
for (Class<?> arg1 : parameterTypes) {
LOG.error("Constructor arg: "+arg1.getName());
}
}
}
/*
* ###################### END DEBUG BLOCK ######################
*/
throw new SettingException(clazz+" does not have a constructor with a single argument");
如果您考虑上面的代码,那么已经添加了调试块,以尝试了解这里发生了什么。如果您暂时忽略了这一点,那么您所拥有的是一个代码块,它从代码中获取一个构造函数数组,并对它们进行迭代,寻找一个只有一个参数的代码。如果循环没有找到,就会抛出异常。
使用添加的异常块,日志显示:
E/Proference: 10/7 22:28:59.917 e.b126:我们从WifiScanning“测试”集中获得了一个WifiScanning的构造函数:true:1E/Proference:10/722:28:59.917 e.b132:不可用构造函数: com.domloge.proference.setting.WifiScanning() V/Proference:清除日志文件E/Proference: 10/7 22:28:59.918 e.b133: From: class com.domloge.proference。setting.WifiScanning E/Proference: 10/7 22:28:59.919 e.b134: e.b134: 10/7 22:28:59.919 e.b135: private=false E/Proference: 10/7 22:28:59.920 e.b136: protected=false E/Proference: 10/7 22:28:59.920 e.b137: public=true E/Proference: 10/7 22:28:59.920 e.b138: static=false E/Proference: 10/7 22:28:59.921 e。b141:构造函数有0个泛型参数类型E/Proference: 10/7 22:28:59.921 e.b147:构造函数有0参数E/Proference: 10/7 22:28:59.922 j.b270:无法应用
如您所见,VM显示的是WifiScanning类提供的一个构造函数,而不是2。这怎么可能呢?
当我在我的个人设备和各种模拟器上运行代码时,这不是一个问题,数组中有两个构造函数。
问题是,当应用程序通过Google商店分发时,数组包含一个构造函数。当我的应用程序在Google商店发布时,我无法调试它,我只能查看日志。
这个校长和应用程序中的其他10多个类一起工作很好.但这件事出问题了..。这就好像有一个错误,我错过了,这将是一个前额拍打哦!当有人指出我的愚蠢错误..。
有什么想法吗?
发布于 2014-08-11 04:11:44
是的,这显然是“麦克西夫”。
将下面的行放在proguard-project.txt中
-keepclassmembers class * extends full-package-name.AbstractSetting {
public protected <init>(...);
}
你会得到它的。
https://stackoverflow.com/questions/25234128
复制相似问题