java - Android - 类的空构造函数也会给出错误

标签 java android inheritance

我从另一个类继承了一个类,首先我未能提供空构造函数。但是在编写空构造函数后,我的应用程序仍然给出相同的错误。

错误:

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.

引用Documentation

关于java - Android - 类的空构造函数也会给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16392602/

相关文章:

java - 高效地检查和设置变量

java - 如何在java中显示一个实例而不是显示三个实例?

c# 另一个泛型的泛型类

java - 为什么继承的方法使用不重新定义的变量?

java - 使用 PostgreSQL 和 Spring Boot 时是否有规则禁止将其实体类命名为 "User"?

java - 从 Java 5 开始不再需要 serialVersionUID?

android - EnableListAdapter 中的上下文

android - ffmpeg解码后的帧存储在哪里?

android - Android.mk中TARGET_ARCH_ABI是怎么设置的

c++ - 访问派生类中的基类成员