我的申请无法启动。我的Firebase.setAndroidContext()
方法中的onCreate()
导致NoSuchMethodException。见下文:
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);
}
我还设置了一个应用程序:
public class FirebaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
这导致来自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
从堆栈跟踪中,您可以看到实际导致崩溃的代码是Firebase.setAndroidContext(getApplicationContext());
我在这里做错了吗?这就是这些例子的用法,所以我有点困惑。
答案 0 :(得分:5)
如Firebase API文档中所述,应在创建或使用任何Firebase参考之前初始化Firebase。您应该在应用程序的 setAndroidContext
方法中使用onCreate()
方法。所以你应该像这样创建自己的应用程序类:
public class FirebaseApplication extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
Firebase.setAndroidContext(this);
}
}
然后将其添加为application
中的AndroidManifest
标记的名称:
<application
android:name="your.package.name.FirebaseApplication"
//android:icon, android:label, android:theme, etc.
... >
...
</application>
然后您可以在活动中使用Firebase
:
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);
}
修改强>
所以我不知道你的问题是什么,因为我能够使用Firebase成功构建项目,例如。但我找到了一件可能对你有所帮助的事情。尝试将下一行添加到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);
}