Lucio Lucio - 4 months ago 14
Java Question

java.lang.RuntimeException with Facebook Login example

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)

Answer

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