There are several related questions on StackOverflow, but none seem to solve my problem.
Consider the following code:
public class MainActivity extends Activity {
// ...
private class MyClass {
protected void myMethod() {
// ...
MyExtendedClass var = (MyExtendedClass)Class.forName("com.example.myapp.MainActivity$MyExtendedClass").newInstance();
// ...
}
}
private class MyExtendedClass extends MyClass {
// ...
}
}
When the line initializing var
is called, I get the following error:
java.lang.InstantiationException: can't instantiate class com.example.myapp.MainActivity$MyExtendedClass; no empty constructor
I cannot figure out why this is happening.
Even if I add the following constructors to their corresponding classes, I still get the same error:
public MyClass() {
// nothing
}
public MyExtendedClass() {
super();
}