As per these instructions https://developers.facebook.com/docs/sharing/android I have implemented the Facebook ShareDialog to appear in my app but it's causing the following error.
03-27 16:12:53.150: E/AndroidRuntime(10275): FATAL EXCEPTION: main 03-27 16:12:53.150: E/AndroidRuntime(10275): Process: au.com.elegantmedia.emotit, PID: 10275 03-27 16:12:53.150: E/AndroidRuntime(10275): java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference 03-27 16:12:53.150: E/AndroidRuntime(10275): at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.Utility.getDialogFeatureConfig(Utility.java:859) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.DialogPresenter.getVersionSpecForFeature(DialogPresenter.java:248) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.DialogPresenter.getProtocolVersionForNativeDialog(DialogPresenter.java:234) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.DialogPresenter.canPresentNativeDialogWithFeature(DialogPresenter.java:75) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.share.widget.ShareDialog.canShowNative(ShareDialog.java:133) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.share.widget.ShareDialog.access$0(ShareDialog.java:130) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:241) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.share.widget.ShareDialog$NativeHandler.canShow(ShareDialog.java:1) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.FacebookDialogBase.createAppCallForMode(FacebookDialogBase.java:184) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.FacebookDialogBase.showImpl(FacebookDialogBase.java:147) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.FacebookDialogBase.show(FacebookDialogBase.java:142) 03-27 16:12:53.150: E/AndroidRuntime(10275): at au.com.elegantmedia.emotit.activities.MainActivity.onTellAFriendClick(MainActivity.java:299) 03-27 16:12:53.150: E/AndroidRuntime(10275): at au.com.elegantmedia.emotit.activities.MainActivity.onItemClick(MainActivity.java:194) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.widget.AdapterView.performItemClick(AdapterView.java:300) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.widget.AbsListView.performItemClick(AbsListView.java:1143) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3044) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.widget.AbsListView$3.run(AbsListView.java:3833) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.os.Handler.handleCallback(Handler.java:739) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.os.Handler.dispatchMessage(Handler.java:95) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.os.Looper.loop(Looper.java:135) 03-27 16:12:53.150: E/AndroidRuntime(10275): at android.app.ActivityThread.main(ActivityThread.java:5221) 03-27 16:12:53.150: E/AndroidRuntime(10275): at java.lang.reflect.Method.invoke(Native Method) 03-27 16:12:53.150: E/AndroidRuntime(10275): at java.lang.reflect.Method.invoke(Method.java:372) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 03-27 16:12:53.150: E/AndroidRuntime(10275): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
At line,
03-27 16:12:53.150: E/AndroidRuntime(10275): at com.facebook.internal.Utility.getDialogFeatureConfig(Utility.java:859)
is the following piece of code,
FetchedAppSettings settings = fetchedAppSettings.get(applicationId);
Both fetchedAppSettings
- {}
and applicationId
- null
seems to be not initialised.
The only place the applicationId is, is in the manifest file.
<provider android:authorities="com.facebook.app.FacebookContentProviderXXX"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
XXX being the applicationId and I have put it there correctly. Everything else also done such as,
FacebookSdk.sdkInitialize(getApplicationContext());
callBackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(this);
shareDialog.registerCallback(callBackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Result result) {
ELog.d(LOG_TAG, "success");
}
@Override
public void onError(FacebookException error) {
ELog.d(LOG_TAG, "error");
}
@Override
public void onCancel() {
ELog.d(LOG_TAG, "cancel");
}
});
in onCreate()
and calling,
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription(
"The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(
Uri.parse("http://developers.facebook.com/android"))
.setImageUrl(Uri.parse("https://fbcdn-dragon-a.akamaihd.net/hphotos-ak-xfa1/t39.2178-6/11057086_1577191859234204_214246289_n.png"))
.build();
shareDialog.show(linkContent);
}
when user clicks to share content. Is there something that I have missed? Or am I running into some sort of a bug in the new SDK which causes the NullPointerException
?
(hopefully someone at FB will read this)
In addition to the (correct) advice given by Gokhan Caglar, also note this: Do not write the app-id number directly into AndroidManifest.xml! For FB to work, app-id must also be defined properly according to best practice in the file strings.xml
Because the app-id is also used under the provider tag, but there directly as the number, I did the shortcut of writing the app-id number directly into the meta-data tag as well. Huge mistake, 2 days gone.
As it seems, FB will make use of app-id fetched from both AndroidManifest.xml and directly based on the definitions in strings.xml. Both must be there. app-id must be defined in strings.xml. This solved the issue.
For FB development team, a couple of suggestions that will save people’s time:
Either remove this over-stringent use of app-id, or explain in the tutorial that this is the only way. (It is more stringent than Android itself)
Make a better error reporting system as overlay when the app definitions are not proper. A null exception like this deep within FB soon becomes your time thief of the day. In this case, an error report of the type: ”Error: app-id not defined in strings.xml” would have been satisfactory.
Regarding the tutorial, it contains a trap also, and so I mention it: If you run the tutorial here: https://developers.facebook.com/quickstarts/?platform=android, after you have already inserted key hash for development, something you would easily do if things aren’t working, then be aware that this tutorial will delete (or rather replace) vital info in the app definition without telling you about it. Since you have perhaps already defined the keyhash, you do not fill it in once again in this quickstart guide. Hence, later you discover that the keyhash is gone and wonder why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With