26

I'm following the Facebook guide to make a login app for Android. After:

  1. Install Facebook app (unnecessary) on the virtual device
  2. Import Facebook SDK into Android Studio as Module
  3. Import the previous module into the project and
  4. Modify the code as the guide says

the project compiles. But when I run the machine and try to open the application it crash showing:

Unfortunately, the application has stopped.

I couldn't find a solution after read similar posts like this and this.

I'm using Android 0.5.8, Oracle JDK 7 and the API 16 as target, min and max.

The main files are activity_main.xml, MainActivity.java and MainFragment.java.

Here is the latest logcat output:

05-30 03:15:54.127      647-664/com.jdk8.minifacebookloginapp.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:781)
            at com.facebook.internal.Utility.queryAppSettings(Utility.java:372)
            at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:676)
            at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:673)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
Community
  • 1
  • 1
Lucio
  • 4,753
  • 3
  • 48
  • 77

3 Answers3

69

The problem is not coming from your code but from the facebook SDK. You can tell by looking at the top of the NullPointerException

Caused by: java.lang.NullPointerException
        at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:781)
        at com.facebook.internal.Utility.queryAppSettings(Utility.java:372)

The error last two triggering lines are from the facebook sdk (com.facebook.internal) and your java.util package neither of which are from your code.

To fix this, you need to finish the setup in the facebook's getting started tutorial for android where you register your app after generating an android key hash. After you've done that, open your AndroidManifest.xml and add the meta-data line

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Then add your app_id into the strings.xml file (found from the settings page in your facebook developer account The app id is on your facebook developer account.

Once you've done this it should compile

Todd Anderson
  • 1,138
  • 9
  • 21
  • You're right. But I'm still missing something. The application launch, but it crash when I click on the "Login" button. It gives [this error](http://paste.ubuntu.com/7605022/), but I think that it is another issue (I should create another question). Is this related? – Lucio Jun 07 '14 at 00:33
  • 1
    I solved the previous error with procedure mentioned [here](http://stackoverflow.com/q/13509113/1505348) and [here](http://stackoverflow.com/q/2169294/1505348). – Lucio Jun 07 '14 at 01:31
  • 1
    This was my problem, and this fixed it. :) – pBlack Jun 19 '14 at 23:54
  • 1
    this is perfect! why is this not ever mentioned on the facebook developer pages? – Mars Jan 18 '15 at 17:15
  • @Todd Anderson can you provide sample application for facebook button widget and login – ManishSB Apr 10 '15 at 21:00
  • The facebook SDK should at least add a null check to show a clear exception of what went wrong in this case – sam Nov 14 '15 at 20:20
3
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

was missing in my case.

Vihaan Verma
  • 12,815
  • 19
  • 97
  • 126
0

You need to add

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

at right place inside the application not inside the activity