【问题标题】:NoSuchMethodException on Firebase.setAndroidContext()Firebase.setAndroidContext() 上的 NoSuchMethodException
【发布时间】:2014-11-07 06:54:48
【问题描述】:

我的应用程序无法启动。我的 onCreate() 方法中的 Firebase.setAndroidContext() 导致 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

从stacktrace可以看出,实际导致crash的代码是Firebase.setAndroidContext(getApplicationContext());

我在这里做错了吗?这是示例使用的,所以我有点困惑。

【问题讨论】:

    标签: java android firebase


    【解决方案1】:

    如 Firebase API 文档中所述,应在创建或使用任何 Firebase 引用之前初始化 Firebase。您应该在 Application 的 onCreate() 方法中使用setAndroidContext 方法。所以你应该像这样创建自己的应用程序类:

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

    【讨论】:

    • 我明白了。当我应该在应用程序中完成它时,我正在我的 Activity 中初始化 Firebase。谢谢。
    • 这最终对我不起作用。问题是Firebase.setAndroidContext(this); 正在触发 NoSuchMethodException。我对上面的问题进行了编辑。观察看看我是否做得对。
    • 您是否从您的MainActivity 中删除了Firebase.setAndroidContext(getApplicationContext());
    • 我确实删除了它。我对原始问题进行了更改以反映它目前的情况。但是,根据 Firebase 文档,它可以在您的 Activity 或您的应用程序中。
    • 是的,它可以在 Activity 中,但在创建任何引用之前。那么,您能否使用新异常更新您的 logcat,当您将 setAndroidContext 移动到应用程序时会出现该异常?因为我可以看到这是你的旧日志。
    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-31
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多