Facebook Android SDK & java.lang.NullPointerException
Asked Answered
H

6

12

I am having trouble opening an active facebook session in my android app as per the tutorial posted at https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

I have been struggling with this for quite some time now without any luck! I will be glad if one of you would point me in the right direction! (I bet the error is with some setup on my side, but I am unable to spot it)

Here is the error I am seeing after calling Session.openActiveSession(this, true, new Session.StatusCallback() in the onCreate of my activity (I am pretty much following the steps given in facevook developer tutorials)


EDIT : The following warning message is logged first and followed by the error message in the subsequent screen shot

09-03 19:26:22.726: W/Bundle(17458): Key com.facebook.platform.protocol.PROTOCOL_VERSION expected String but value was a java.lang.Integer. The default value was returned. 09-03 19:26:22.736: W/Bundle(17458): Attempt to cast generated internal exception: 09-03 19:26:22.736: W/Bundle(17458): java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String 09-03 19:26:22.736: W/Bundle(17458): at android.os.Bundle.getString(Bundle.java:1061) 09-03 19:26:22.736: W/Bundle(17458): at android.content.Intent.getStringExtra(Intent.java:4466) 09-03 19:26:22.736: W/Bundle(17458): at com.facebook.AuthorizationClient$KatanaLoginDialogAuthHandler.tryAuthorize(AuthorizationClient.java:821) 09-03 19:26:22.736: W/Bundle(17458): at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:272)

enter image description here


09-02 22:55:04.110: E/AndroidRuntime(13287): FATAL EXCEPTION: main 09-02 22:55:04.110: E/AndroidRuntime(13287): java.lang.RuntimeException: Unable to resume activity {com.good.amrfbintegration/com.facebook.LoginActivity}: java.lang.NullPointerException 09-02 22:55:04.110: E/AndroidRuntime(13287): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3036) 09-02 22:55:04.110: E/AndroidRuntime(13287): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3077) 09-02 22:55:04.110: E/AndroidRuntime(13287): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2373)

enter image description here

Here is what I have done so far

Updated the App on Facebook with the following information (I have used the hashkey generated using my machine's debug keystore)

enter image description here

Here are the relevant info from my manifest file (@string/app_id is the App Id from facebook dev portal)

enter image description here

I added a reference to the Facebook SDK to my project in this way

enter image description here

Here is the code in my OnCreate method of the main activity

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

        Session.openActiveSession(this, true, new Session.StatusCallback() 
        {
            @Override
            public void call(Session session, SessionState state, Exception exception)
            {               
                if (session.isOpened())
                {
                    Toast.makeText(activity, "Connected to Fb", Toast.LENGTH_SHORT).show();
                }
            }});
        }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

On executing the Session.openActiveSession, logcat display the error shown in my first screenshot.

The Session.StatusCallback()'s call method gets executed once (while session has a state 'opening') but the app crashes the subsequent time. here is what I see before the App crashes

enter image description here

Any assistance would be greatly appreciated!

Hoodmanblind answered 3/9, 2013 at 4:2 Comment(5)
It could be because of Proguard. It was working flawlessly on all of my devices until i enabled Proguard. I'm still dealing haplessly with this issue. Let me know if you found a way out..Googolplex
I got the same issue today. And this was due to the APP_ID mismatch in manifest. com.facebook.platform.protocol.PROTOCOL_VERSION expected String but value was a java.lang.Integer. The default value was returned.Synecdoche
Did you figure out what the issue was? I've been having the same issue for days, and I'm pulling my hair trying to figure out what's going.Escurial
Tony - Tomorrow, I will post the code snippet that works fine on my App (I feel annoyed to tell this... but YMMV). Moreover, I think antelope's solution below could be something that you should try..Hoodmanblind
Well, I updated my app to use the latest facebook SDK (3.5.2) and authentication appears to be working fine nowHoodmanblind
D
7

This problem because of Facebook SDK, please download and use Facebook SDK 3.5.2 from this URL:-https://developers.facebook.com/docs/android/

Dictate answered 12/11, 2013 at 11:29 Comment(0)
S
3

Yes. Check your code,

Make sure, you have these things on Manifest.xml

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

And @string/app_id should contain the app_id that you get from your facebook app configuration details. Also there you should put the correct keyhash.

So make sure -

  1. your key hash is correct
  2. You have configured app correctly on web, and taken care of putting the app id in your app configuration.
Synecdoche answered 6/9, 2013 at 8:34 Comment(0)
C
2

It happened to me today as well.

My problem was that the Facebook app was in sandbox mode. So only Developers and Testers could use it.

Change it from the Facebook Developer properties page of your app: https://developers.facebook.com/apps

Also found my answer here: Can not access Facebook session - UnknownError: Invalid application

Clustered answered 16/9, 2013 at 6:36 Comment(0)
A
0
// Just write your code in onResume() rather than onCreate().
@Override
public void onResume() {
    super.onResume();

      if(Session.getActiveSession()==null){
          session = new Session(this);
          Session.setActiveSession(session);
     }
     if (Session.getActiveSession().isOpened())
     {
          Toast.makeText(activity, "Connected to Fb", Toast.LENGTH_SHORT).show();
     }else{
        Session.openActiveSession(this, true, new Session.StatusCallback() 
        {
          @Override
          public void call(Session session, SessionState state, Exception exception)
          {               
            if (session.isOpened())
            {
                Toast.makeText(activity, "Connected to Fb", Toast.LENGTH_SHORT).show();
            }
          }});
       }
   }
}
Abrasive answered 3/9, 2013 at 4:14 Comment(1)
It did not fix it - I am running into the same error! The sample app in developers.facebook.com/docs/android/getting-started/… also does it in the OnCreate methodHoodmanblind
H
0

I dug a bit into the SDK (3.5) and it seems that EXTRA_PROTOCOL_VERSION is always stored as an Integer not as a String. I.e. in NativeProtocol.java:

 public static Intent createPlatformActivityIntent(Context context, String action, int version, Bundle extras) {
    Intent intent = new Intent()
            .setAction(INTENT_ACTION_PLATFORM_ACTIVITY)
            .setPackage(FACEBOOK_PACKAGE)
            .addCategory(Intent.CATEGORY_DEFAULT)
            .putExtras(extras)
            .putExtra(EXTRA_PROTOCOL_VERSION, version) //version --> int
            .putExtra(EXTRA_PROTOCOL_ACTION, action);
    return validateKatanaActivityIntent(context, intent);
}

public static Intent createPlatformServiceIntent(Context context) {
    Intent intent = new Intent(INTENT_ACTION_PLATFORM_SERVICE)
            .setPackage(FACEBOOK_PACKAGE)
            .addCategory(Intent.CATEGORY_DEFAULT);
    return validateKatanaServiceIntent(context, intent);
}

public static Intent createLoginDialog20121101Intent(Context context, String applicationId, ArrayList<String> permissions,
        String audience) {
    Intent intent = new Intent()
                .setAction(INTENT_ACTION_PLATFORM_ACTIVITY)
                .setPackage(FACEBOOK_PACKAGE)
                .addCategory(Intent.CATEGORY_DEFAULT)
                .putExtra(EXTRA_PROTOCOL_VERSION, PROTOCOL_VERSION_20121101) // PROTOCOL_VERSION_20121101 --> int
                .putExtra(EXTRA_PROTOCOL_ACTION, ACTION_LOGIN_DIALOG)
                .putExtra(EXTRA_APPLICATION_ID, applicationId)
                .putStringArrayListExtra(EXTRA_PERMISSIONS, ensureDefaultPermissions(permissions))
                .putExtra(EXTRA_PROTOCOL_CALL_ID, generateCallId())
                .putExtra(EXTRA_WRITE_PRIVACY, ensureDefaultAudience(audience));
    return validateKatanaActivityIntent(context, intent);
}

And at the place where devs usually get this error is read as a String (AuthorizationCLient.java l:820):

addLoggingExtra(EVENT_EXTRAS_PROTOCOL_VERSION,
                intent.getStringExtra(NativeProtocol.EXTRA_PROTOCOL_VERSION));

That will not work in any case so it's still a bug in the SDK. It is just that under some circumstances, that segment gets executed.

Any comments?

Heterozygote answered 2/10, 2013 at 10:52 Comment(1)
creating a new function with int input, would normally work in this case shouldn't it? i am dealing with the same problem...Perfuse
I
0

I was getting the same error,google'd lot of things and finally this is what I did to get away from that error

  private void getHashKey()
   {
           PackageInfo info;
           try {
           info = getPackageManager().getPackageInfo("Your_Package_Name", PackageManager.GET_SIGNATURES);
           for (Signature signature : info.signatures) {MessageDigest md;
           md = MessageDigest.getInstance("SHA");
           md.update(signature.toByteArray());
                      //String something = new String(Base64.encode(md.digest(), 0));
                        String something = new String(Base64.encode(md.digest(),0));
                      Log.e("** Hash Key", something);
           }
           }
           catch (NameNotFoundException e1) {
           Log.e("name not found", e1.toString());
           }

           catch (NoSuchAlgorithmException e) {
           Log.e("no such an algorithm", e.toString());
           }
           catch (Exception e){
           Log.e("exception", e.toString());
           }

   }

So,I used the Hash_Key generated by this code,and updated it in my facebook developer console. This solved my problem.Probably,some issues with windows key_gen or the android key_store

And also,do check your AuthorizationClient.java file from Facebook Sdk at line 820

addLoggingExtra(EVENT_EXTRAS_PROTOCOL_VERSION,"" + 
                    intent.getIntExtra(NativeProtocol.EXTRA_PROTOCOL_VERSION, 0));

I made it to "getIntExtra",where they had it as "getStringExtra". Making these changes,worked for me. Hope it is gonna work for you too.

Idiopathy answered 6/5, 2014 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.