Facebook login NullPointerException

Ojonugwa Jude Ochalifu picture Ojonugwa Jude Ochalifu · Aug 12, 2014 · Viewed 41.1k times · Source

Am trying to implement the Facebook login for Android using the Facebook SDK (3.17.1) but i am having some issues i haven't had in the past. First of all, the login button doesn't render completely. This is what i get

enter image description here

instead of this:

enter image description here

(notice the missing Facebook logo). When I run the application, in the split second before it crashes it displays it just the way it should (with the Facebook logo).

When the application crashes, i get this error message

"Unfortunately, FacebookLoginTest as stopped"

and this in Logcat:

08-12 22:19:08.751: E/AndroidRuntime(7731): FATAL EXCEPTION: AsyncTask #1
08-12 22:19:08.751: E/AndroidRuntime(7731): Process: com.example.facebooklogintest,    PID: 7731
08-12 22:19:08.751: E/AndroidRuntime(7731): java.lang.RuntimeException: An error   occured while executing doInBackground()
08-12 22:19:08.751: E/AndroidRuntime(7731):     at     android.os.AsyncTask$3.done(AsyncTask.java:300)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at  java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at  java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at  java.lang.Thread.run(Thread.java:864)
08-12 22:19:08.751: E/AndroidRuntime(7731): Caused by: java.lang.NullPointerException
08-12 22:19:08.751: E/AndroidRuntime(7731):     at   java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:911)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at   com.facebook.internal.Utility.queryAppSettings(Utility.java:377)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:677)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:1)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-12 22:19:08.751: E/AndroidRuntime(7731):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-12 22:19:08.751: E/AndroidRuntime(7731):     ... 4 more
08-12 22:19:10.753: D/Process(7731): killProcess, pid=7731
08-12 22:19:10.753: D/Process(7731):   com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:131    java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

I followed the instructions for adding Facebook login to Android apps:

  1. Imported Facebook sdk
  2. Added Facebook project as libray
  3. Added Facebook project to list of references for my project
  4. Cleaned and even restarted the project.

Besides these, I haven't changed anything in my Activity and manifest. I have tried this in three different eclipse versions, all with the same error, but then i delete the Facebook login button code in the .xml file, the app lauches just fine. Not sure what else am supposed to do. Here are the MainActivity and Layout.xml codes:

MainActivity.java

package com.example.facebooklogintest;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.facebooklogintest.MainActivity" >



<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp" />

 </RelativeLayout>

Answer

apmartin1991 picture apmartin1991 · Aug 13, 2014

You need to change or add this in your manifest file

 <activity
        android:name="com.example.LoginActivity"
        android:label="@string/app_name" 
        android:parentActivityName="com.example.MainActivity" >


 </activity>
 <activity android:name="com.facebook.LoginActivity"
              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              android:label="@string/app_name" />
    <meta-data android:name="com.facebook.sdk.ApplicationId"   android:value="@string/app_id"/>

I cannot take credit for this, all credit must go to the guy who posted the answer here I am purely adding the code for ease for you and because I got told to by someone who previously deleted the answer.