Facebook Android SDK v4.0.0 Problem with ShareDialog NullPointerException

In accordance with these instructions https://developers.facebook.com/docs/sharing/android I have implemented Facebook ShareDialog to display in my application, but this caused 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 call the virtual method 'int java.lang.Object.hashCode ()' in the reference to the null object 03-27 16: 12: 53.150 : E / AndroidRuntime (10275): in 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): on 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.i nternal.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.canShowS (ShareDialog.java:1) 03-27 16: 12: 53.150: E / AndroidRuntime (10275): on com.facebook.internal.FacebookDialogBase.createAppCallForMode (FacebookDialogBase.java:184) 03-27 16: 12: 53.150: E / AndroidRuntime (10275): on com.facebook.internal.FacebookDialogBase.showImpl (FacebookDialogBase.java:147) 03-27 16: 12: 53.150: E / AndroidRuntime (10275): on 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): on 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.javahaps72) 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)

In line

03-27 16: 12: 53.150: E / AndroidRuntime (10275): at com.facebook.internal.Utility.getDialogFeatureConfig (Utility.java:859)

- the following code fragment,

FetchedAppSettings settings = fetchedAppSettings.get(applicationId); 

Both fetchedAppSettings - {} and applicationId - null seem to be uninitialized.

The only place applicationId is in the manifest file.

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

XXX is applicationId and I put it there correctly. Everything else is also done, for example,

 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 call,

 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 a user clicks on content sharing. Is there something I missed? Or am I encountering some kind of error in the new SDK that throws a NullPointerException ?

+6
source share
3 answers

You also need to add the application identifier as follows:

 <application android:label="@string/app_name" ...> ... <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/> ... </application> 
+14
source

(hope someone from the FB reads this)

In addition to the (correct) advice given by Gokhan Caglar, also pay attention to this: Do not write the id app number directly in AndroidManifest.xml! For FB to work, app-id must also be correctly defined in accordance with best practice in the strings.xml file

Since the application identifier is also used in the provider tag, but directly as a number, I made a shortcut to write the application identifier number directly in the metadata tag. A huge mistake, after 2 days.

It seems that FB will use the app-id extracted from AndroidManifest.xml and directly based on the definitions in the strings.xml file. Both must be there. app-id must be defined in the strings.xml file. This solved the problem.

There are several suggestions for the FB development team that will save people time:

  • Either remove this overly restrictive use of app-id, or explain in the tutorial that this is the only way. (It is more strict than Android itself)

  • Create a better error reporting system as an overlay if the application definitions are not correct. A null exception like this deep inside the FB will soon become your thief of the day. In this case, an error report of the type "Error: app-id not defined in strings.xml" would be satisfactory.

  • As for the tutorial, it also contains a trap, and so I mention this: if you run the tutorial here: https://developers.facebook.com/quickstarts/?platform=android , the key hash for development already inserted is what you could easily be done if things worked, then keep in mind that this tutorial will remove (or rather replace) important information in the application definition without telling you about it. Since you may already have identified keyhash, you will not fill it again in this quick start guide. Therefore, later you will find that keyhash is gone and wondered why.

+19
source

do not write the facebook key in the meta tag

write it to a string and refer to it in a meta in the manifest

So

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

hope this helps :)

+1
source

Source: https://habr.com/ru/post/984316/


All Articles