Search code examples
javaandroidfacebookfacebook-sdk-4.0android-studio-2.3

Android Share to Facebook not working


I'm trying to share a content URL and send it over to facebook. I've been following the official documentation I have the dependencies in grade and I have the following in my Manifest file

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

  <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name" />

  <provider android:authorities="com.facebook.app.FacebookContentProvider247315745828416"
        android:name="com.facebook.FacebookContentProvider"
        android:exported="true"/>
</application>

I can log in to my app using facebook fine. In my code I call the share content by doing the following

private void uploadToFacebook(){

    ShareLinkContent content = new ShareLinkContent.Builder()
            .setContentUrl(Uri.parse("https://developers.facebook.com"))
            .build();
    shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);
    toastMessage("Done");

}

When I run the code without shareDialog.show() the toast message appears but the url is not send over to my facebook wall.

When I inclide shareDialog.show() the application crashes and I get a null pointer exception on it.

The content is definitely an object as I get a value back when I convert it to a string I get

com.facebook.share.model.ShareLinkContent@c63dc77

I am using android version 2.3.3 any help would be greatly appreciated as I can't seem to grasp what I've done wrong. I have a key hash stored for as well.


Solution

  • So I found the answer.

    In my on create I never added the following to the on create

    shareDialog = new ShareDialog(this);
    

    This is not in the tutorial. For all others using the tutorial create the content then in the same function the content is created below where the content is created add

    shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);
    

    To share the content