1

My application is isn't able to start. Firebase.setAndroidContext() in my onCreate() method is causing a NoSuchMethodException. See below:

protected Firebase ref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String firebaseUrl = getResources().getString(R.string.firebase_url);
    Firebase.setAndroidContext(getApplicationContext());
    ref = new Firebase(firebaseUrl);

} 

I also setup an application:

public class FirebaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Firebase.setAndroidContext(this);
    }
}

Which leads to the following stacktrace from logcat:

I/sf_frame_dur(   60): [com.android.launcher/com.android.launcher2.Launcher,0,0,0,20,42,40,20]
D/AndroidRuntime(14165): Shutting down VM
E/AndroidRuntime(14165): FATAL EXCEPTION: main
E/AndroidRuntime(14165): Process: com.github.r351574nc3.earshot, PID: 14165
E/AndroidRuntime(14165): java.lang.AssertionError: impossible
E/AndroidRuntime(14165):    at java.lang.Enum$1.create(Enum.java:45)
E/AndroidRuntime(14165):    at java.lang.Enum$1.create(Enum.java:35)
E/AndroidRuntime(14165):    at libcore.util.BasicLruCache.get(BasicLruCache.java:54)
E/AndroidRuntime(14165):    at java.lang.Enum.getSharedConstants(Enum.java:211)
E/AndroidRuntime(14165):    at java.lang.Class.getEnumConstants(Class.java:1029)
E/AndroidRuntime(14165):    at com.fasterxml.jackson.databind.cfg.MapperConfig.collectFeatureDefaults(MapperConfig.java:73)
E/AndroidRuntime(14165):    at com.fasterxml.jackson.databind.cfg.MapperConfigBase.<clinit>(MapperConfigBase.java:28)
E/AndroidRuntime(14165):    at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:433)
E/AndroidRuntime(14165):    at com.fasterxml.jackson.databind.ObjectMapper.<init>(ObjectMapper.java:364)
E/AndroidRuntime(14165):    at com.firebase.client.Firebase.<clinit>(Firebase.java:41)
E/AndroidRuntime(14165):    at com.firebase.client.Firebase.setAndroidContext(Firebase.java:860) 
E/AndroidRuntime(14165):    at com.github.r351574nc3.earshot.EarshotApplication.onCreate(EarshotApplication.java:11)
E/AndroidRuntime(14165):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1008)
E/AndroidRuntime(14165):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4397)
E/AndroidRuntime(14165):    at android.app.ActivityThread.access$1500(ActivityThread.java:143)
E/AndroidRuntime(14165):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
E/AndroidRuntime(14165):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(14165):    at android.os.Looper.loop(Looper.java:135)
E/AndroidRuntime(14165):    at android.app.ActivityThread.main(ActivityThread.java:5070)
E/AndroidRuntime(14165):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(14165):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime(14165):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
E/AndroidRuntime(14165):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
E/AndroidRuntime(14165): Caused by: java.lang.NoSuchMethodException: values []
E/AndroidRuntime(14165):    at java.lang.Class.getMethod(Class.java:664)
E/AndroidRuntime(14165):    at java.lang.Class.getDeclaredMethod(Class.java:626)
E/AndroidRuntime(14165):    at java.lang.Enum$1.create(Enum.java:41)
E/AndroidRuntime(14165):    ... 22 more

From the stacktrace, you can see that the code that is actually causing the crash is Firebase.setAndroidContext(getApplicationContext());

Am I doing something wrong here? This is what the examples use, so I'm kinda confused.

r351574nc3
  • 73
  • 1
  • 7

1 Answers1

5

As mentioned in Firebase API docs, the Firebase should be initialized before any Firebase reference is created or used. You should use setAndroidContext method in Application's onCreate() method. So you should create your own application class like this:

public class FirebaseApplication extends android.app.Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Firebase.setAndroidContext(this);
    }
}

Then add it as name of your application tag in AndroidManifest:

 <application
        android:name="your.package.name.FirebaseApplication"
        //android:icon, android:label, android:theme, etc.
        ... >
      ...
  </application>

And then you can use Firebase in your activities:

protected Firebase ref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String firebaseUrl = getResources().getString(R.string.firebase_url);
    ref = new Firebase(firebaseUrl);

} 

EDIT:

So I don't know what's your problem, cause I was able to successfully build the project with Firebase, as in example. But I found one thing, that may be helpful for you. Try to add next lines to the proguard-rules.pro:

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
romtsn
  • 11,704
  • 2
  • 31
  • 49
  • I see. I was initializing Firebase in my Activity when I should have done it in the Application. Thanks. – r351574nc3 Nov 07 '14 at 19:12
  • This ended up not working for me. The problem is that `Firebase.setAndroidContext(this);` is triggering a NoSuchMethodException. I made edits to my problem above. Observe and see if I did it correctly. – r351574nc3 Nov 07 '14 at 19:48
  • Did you remove `Firebase.setAndroidContext(getApplicationContext());` from your `MainActivity` ? – romtsn Nov 07 '14 at 19:52
  • I did remove it. I made the changes to my original question to reflect what it currently is. However, according to the Firebase docs it can be in your Activity or in your Application. – r351574nc3 Nov 07 '14 at 21:21
  • Yes, it can be in the Activity, but **before any reference is created**. So, can you update your logcat with new exception, that appears when you move `setAndroidContext` to the Application? Because I can see that it's your old Log. – romtsn Nov 08 '14 at 05:07
  • Yes, `Firebase.setAndroidContext` is the first thing that gets called for Firebase. I updated the stacktrace. This is pretty much straight out of an example project BTW. I figure it must be something I'm missing like a resource or a dependency or something. I am using android-20 with firebase client 2.0.0 – r351574nc3 Nov 08 '14 at 23:43
  • Proguard change worked for me. Linking in another issue for more background: http://stackoverflow.com/questions/20651575/android-release-apk-crash-with-java-lang-assertionerror-impossible-in-java-lang – r351574nc3 Nov 09 '14 at 08:42