1

I am trying to add a facebook login to my app. Unfortunatly I am getting an error. I defined the app id in my strings.xml and add meta-data to my manifest. Although I added the facebook activity and the permission to use the Internet but the error is still there. I am of the opionen that there is a problem with the layout but I don't see one. Maybe you could help me.

EDIT: I changed the position of the initialization and the content view function but now I am getting a new error. I looked it up and found out that it is normally a problem with the app id but I have implemented the id using meta data in my manifest.

The error is the following:

10-07 10:06:36.249    4682-4748/de.homeproducts.ineedhelp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    Process: de.homeproducts.ineedhelp, PID: 4682
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:304)
            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:818)
     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.get(ConcurrentHashMap.java:746)
            at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
            at com.facebook.internal.Utility.queryAppSettings(Utility.java:802)
            at com.facebook.login.widget.LoginButton$1.doInBackground(LoginButton.java:502)
            at com.facebook.login.widget.LoginButton$1.doInBackground(LoginButton.java:499)
            at android.os.AsyncTask$2.call(AsyncTask.java:292)
            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:818)

This is my Activity class:

public ImageView view = null;
public LoginButton loginButton = null;
public CallbackManager callbackManager = null;

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

    FacebookSdk.sdkInitialize(this.getApplicationContext());

    callbackManager = CallbackManager.Factory.create();
    loginButton = (LoginButton) this.findViewById(R.id.login_button);

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            Toast toast = Toast.makeText(getApplicationContext(), "Login erfolgreich!", Toast.LENGTH_LONG);
        }

        @Override
        public void onCancel() {
            Toast toast = Toast.makeText(getApplicationContext(), "Login abgebrochen!", Toast.LENGTH_LONG);
        }

        @Override
        public void onError(FacebookException e) {
            Toast toast = Toast.makeText(getApplicationContext(), "Login fehlgeschlagen!", Toast.LENGTH_LONG);
        }
});

And this is my XML file:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ImageView android:id="@+id/hello_world" android:src="@drawable/icon" android:layout_width="128dp"
    android:layout_height="128dp" android:layout_centerHorizontal="true"/>

<com.facebook.login.widget.LoginButton
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    />

</RelativeLayout>

The manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.homeproducts.ineedhelp" >

<uses-permission android:name="android.permission.INTERNET"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <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>
    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name" />
</application>

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

</manifest>
Neatoro
  • 173
  • 1
  • 9

1 Answers1

1

You have to write :

FacebookSdk.sdkInitialize(this.getApplicationContext());

Before:

setContentView(R.layout.activity_main);

See Facebook Sdk Has Not Been Initilized

Community
  • 1
  • 1
vaibhav
  • 816
  • 9
  • 13
  • I changed it but now I get the AsyncTask error. Google said, that the appID is missing, but I have already added it. – Neatoro Oct 07 '15 at 08:03