4

I've gotten a number of crash reports with the following stack traces (names changed):

Caused by: java.lang.InstantiationException: can't instantiate class com.example.MyApplication; no empty constructor
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1319)
    at android.app.Instrumentation.newApplication(Instrumentation.java:997)
    at android.app.Instrumentation.newApplication(Instrumentation.java:982)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
    ... 11 more

java.lang.RuntimeException: Unable to instantiate application com.example.MyApplication: java.lang.InstantiationException: can't instantiate class com.example.MyApplication; no empty constructor
    at android.app.LoadedApk.makeApplication(LoadedApk.java:501)

android.app.Application has an explicit parameterless constructor:

public Application() {
  super(null);
}

MyApplication inherits from android.app.Application and has no explicit constructors at all. According to my understanding of the Java spec, this means that the following constructor should be implicitly inserted by the compiler:

public MyApplication() {
  super();
}

This must have happened, or I'd never have been able to compile the app in the first place. So what could be causing these crashes?


EDIT: Here's part of the output from decompiling the ProGuard-ed MyApplication.class with javap:
Compiled from "MyApplication.java"
public class com.example.MyApplication extends android.app.Application {

  public com.example.MyApplication();
    Signature: ()V

  public void onCreate();
    Signature: ()V

  public void onLowMemory();
    Signature: ()V

  public void onTrimMemory(int);
    Signature: (I)V

  // ... some other methods ...

  static {};
    Signature: ()V
}

The default constructor is definitely in there, and it's public.

kpozin
  • 25,691
  • 19
  • 57
  • 76
  • Have you tried declaring the constructor explicitly? Do you still have the problem when doing so? I'm aware it won't explain why this currently does not work, but I'm just being curious here. – Joffrey Apr 28 '14 at 16:05
  • This seems vaguely related: http://stackoverflow.com/questions/21168579/android-instantiationexception-no-empty-constructor – Kevin Krumwiede Apr 28 '14 at 16:10
  • If you have access to the compiled resources, run `javap` and check if such a constructor exists and is public. – Sotirios Delimanolis Apr 28 '14 at 16:16
  • @Joffrey, I've never seen this crash myself, only in crash reports. I could declare the constructor explicitly but it will be a while before I can see if it's fixed the problem "in the wild." – kpozin Apr 28 '14 at 16:17
  • @SotiriosDelimanolis, I added output from `javap` to the question. – kpozin Apr 28 '14 at 17:41
  • do you have details regarding the devices/os version on which those crashes occurred? – njzk2 Apr 28 '14 at 17:43

1 Answers1

1

Note: this turned out not to be the issue in this case, but if you're reading this question because you've faced the same symptom, it could be the issue for you...

According to my understanding of the Java spec, this means that the following constructor should be implicitly inserted by the compiler

public MyApplication() {
    super();
}

That's only the case if it's a public class. From section 8.8.9 of the JLS:

The default constructor has the same accessibility as the class (§6.6).

So if your class is declared as having any access other than public, the constructor won't be public either.

You haven't shown your class declaration, but the first thing you should check is that it's public.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194