0

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

错误:

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);
 }

但这仍然给我同样的错误。有人可以帮忙吗?

4

1 回答 1

2

如果您constructor在类中创建任何自定义,Compiler则不提供默认的 empty constructorconstructor在这种情况下,如果要Object使用 no-argument 创建该类,则必须提供 empty constructor

您不必为您的类提供任何构造函数,但这样做时必须小心。编译器会自动为任何没有构造函数的类提供无参数的默认构造函数。此默认构造函数将调用超类的无参数构造函数。在这种情况下,如果超类没有无参数构造函数,编译器会报错,因此您必须验证它是否存在。如果你的类没有显式的超类,那么它有一个 Object 的隐式超类,它确实有一个无参数的构造函数。

参考文档

于 2013-05-06T05:45:48.163 回答