我一直试图让它工作一段时间,并且我一直在寻找答案(包括这里)。但我无法弄清楚。
所以这里的代码(有点最小化):
public class Event<T> {
public MessageType msgType;
public int layer;
public int gameObjectId;
public String name;
public float value;
public int senderId;
public T ext;
public Event(Class<T> classType) {
if(classType != null) {
try {
ext = classType.newInstance(); //this is the failing line
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
msgType = MessageType.NONE;
layer = TypeHandler.lAll;
gameObjectId = -1;
name = null;
value = -1;
}
}
public class DataExtension {
public float[] values;
public String[] string;
public Object[] objects;
public boolean[] boolens;
}
这就是问题:
Event<DataExtension> mSelectionEvent;
mSelectionEvent = new Event<DataExtension>(DataExtension.class, MessageType.SELECTED);
这给了我以下例外:
java.lang.InstantiationException: se.plainentertainment.bagl.v2.core.Event$DataExtension
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at se.plainentertainment.bagl.v2.core.Event.<init>(Event.java:85)
at se.plainentertainment.bagl.v2.core.SelectionHandler.<init>(SelectionHandler.java:46)...
所以,我试图实例化的类应该满足所有要求,对吧?它有一个默认构造函数,并且它不是抽象的。为什么我会收到此错误?
提前致谢!