Firebase.setAndroidContext(Context)导致FATAL EXCEPTION:AsyncTask#1

时间:2015-01-26 08:02:22

标签: android firebase

只需在我的应用程序的任何位置调用此方法Firebase.setAndroidContext(this)(为了使用Firebase而要做的第一件事)会导致FATAL EXCEPTION: AsyncTask #1

我正在Gradle上运行'com.firebase:firebase-client-android:2.1.1+'

有趣的是,他们提供的演示应用程序有效!和我的代码完全一样! https://github.com/firebase/firebase-login-demo-android

那么,有人可以帮助我吗?

这是完整的堆栈:

    01-26 05:48:00.142  14666-14693/br.com.vymajoris.helpradarprototype1 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: br.com.vymajoris.helpradarprototype1, PID: 14666
    java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:811)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
        at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:911)
        at com.facebook.internal.Utility.queryAppSettings(Utility.java:673)
        at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:678)
        at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:675)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:811)

2 个答案:

答案 0 :(得分:2)

没有任何代码可以帮助您。但你是否通过了应用程序上下文?

创建一个名为 LoginDemoApplication 的类(如示例中所示):

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

然后在应用程序标签下的AndroidManifest.xml中添加:

android:name=".LoginDemoApplication"

然后,您可以像平常一样在您的Activity中创建Firebase参考:

Firebase mFirebaseRef = new Firebase("your_firebase_url);

修改

在mainfests应用程序代码中,请确保

     <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />

使用相应应用程序的AppID在developers.facebook.com

答案 1 :(得分:1)

在我的情况下,我不得不改变

Firebase.setAndroidContext(this); 

Firebase.setAndroidContext(this.getApplication()); 

并修复了它。