我从另一个类继承了一个类,首先我未能提供空构造函数。但是在编写空构造函数后,我的应用程序仍然给出相同的错误。
错误:
java.lang.InstantiationException: can't instantiate class com.example.test3.VideoRecorder; no empty constructor
这是我正在使用的普通构造函数,效果很好:
public Videoplanner(Context ctxt, String logTag,Application app) throws NoSuchAlgorithmException
{
super(ctxt, logTag);
TelephonyManager telephonyManager = (TelephonyManager) ctxt.getSystemService(Context.TELEPHONY_SERVICE);
MessageDigest digester = MessageDigest.getInstance("SHA-1");
byte[] digest = digester.digest(telephonyManager.getDeviceId().getBytes());
hashedID = (new BigInteger(1, digest)).toString(16);
serviceName = hashedID;
context = ctxt;
appl = app;
}
现在错误伴随着空构造函数,这给出了错误
public Videoplanner()
{
super(null, null);
}
我也尝试过
static Context context;
static String str;
public Videoplanner()
{
super(context,str);
}
但这仍然给我同样的错误。有人可以帮忙吗?
最佳答案
如果您在类中创建任何自定义构造函数
,则Compiler
不会提供默认的空构造函数
。在这种情况下,如果您想使用无参数构造函数
创建该类的Object
,则必须提供空构造函数
。
You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.
关于java - Android - 类的空构造函数也会给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16392602/