Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to invoke virtual method 'int java.lang.Object.hashCode()' in facebook integration

I am getting the following error when I am trying facebook integration:

11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: Process: com.creaa.admin.mylogin, PID: 23052
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime:     at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime:     at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime:     at com.facebook.internal.Utility.queryAppSettings(Utility.java:822)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime:     at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:489)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-18 15:57:54.186 23052-23093/com.creaa.admin.mylogin E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)

Below is my main activity:

public class MainActivity extends Activity {

    private static String APP_ID = "1500167156975715"; // Replace your App ID here
    CallbackManager callbackManager;
    ArrayList<String> permissions;
    Context cv = this;
    boolean loginflag = false;
    private ProfileTracker profiletracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //initilise fb sdk
        FacebookSdk.sdkInitialize(getApplicationContext());
        //logout any previous logins
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        accessToken.setCurrentAccessToken(null);
        Profile.getCurrentProfile().setCurrentProfile(null);
        LoginManager.getInstance().logOut();
        setContentView(R.layout.activity_main);
        permissions = new ArrayList<String>();
        permissions.add("email");
        permissions.add("user_likes");
        permissions.add("user_friends");
        permissions.add("public_profile");
        permissions.add("user_birthday");


        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "com.example.facebooklogin",
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("Parth", "KeyHash : " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        } catch (NameNotFoundException e) {

        } catch (NoSuchAlgorithmException e) {

        }
        callbackManager = CallbackManager.Factory.create();
        LoginButton loginbutton = (LoginButton) findViewById(R.id.login_button);
        loginbutton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (AccessToken.getCurrentAccessToken() != null && Profile.getCurrentProfile() != null) {
                    if (loginflag) {
                        AccessToken accessToken = AccessToken.getCurrentAccessToken();
                        accessToken.setCurrentAccessToken(null);
                        Profile.getCurrentProfile().setCurrentProfile(null);
                        Toast.makeText(cv, "Logout", Toast.LENGTH_SHORT).show();
                        LoginManager.getInstance().logOut();
                    }
                } else {
                    if (!loginflag) {
                        Toast.makeText(cv, "Login", Toast.LENGTH_SHORT).show();
                        LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, permissions);
                    }
                }
            }
        });
        loginbutton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

            @Override
            public void onSuccess(LoginResult result) {
                // TODO Auto-generated method stub
                Toast.makeText(cv, "in onSucess", Toast.LENGTH_SHORT).show();
                //get account details
                GraphRequest request = GraphRequest.newMeRequest(result.getAccessToken(), new GraphJSONObjectCallback() {

                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        // TODO Auto-generated method stub
                        Log.d("Parth", response.toString() + "\njson" + object.toString());
                        try {
                            String email = (String) object.get("email");
                            Toast.makeText(cv, email, Toast.LENGTH_SHORT).show();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender,birthday");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onError(FacebookException error) {
                // TODO Auto-generated method stub
                Toast.makeText(cv, "in onError", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel() {
                // TODO Auto-generated method stub
                Toast.makeText(cv, "in oncancel", Toast.LENGTH_SHORT).show();
            }
        });
        profiletracker = new ProfileTracker() {

            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
                // TODO Auto-generated method stub
                if (currentProfile != null) {
                    Toast.makeText(cv, "in Currentprofilechanged", Toast.LENGTH_SHORT).show();
                    String name = currentProfile.getName();
                    String fname = currentProfile.getFirstName();
                    Toast.makeText(MainActivity.this, fname, Toast.LENGTH_SHORT).show();
                }
            }
        };
    }
}

Below is the app gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.creaa.admin.mylogin"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories { mavenCentral() }
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.facebook.android:facebook-android-sdk:4.6.0'
}

Below is the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.creaa.admin.mylogin" >
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <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:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />
        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="1500167156975715"/>
        <provider
            android:name="com.facebook.FacebookContentProvider"
            android:authorities="com.facebook.app.FacebookContentProvider233936543368280"
            android:exported="true" />
    </application>

</manifest>

I have tried all possible things but I can't figure out the error. It would be very kind if anyone could help me with this problem. I have just shifted from eclipse to android studio. This code was working in eclipse just fine but it isn't working in android studio.

like image 793
Parth Anjaria Avatar asked Nov 18 '15 10:11

Parth Anjaria


2 Answers

i have resolve this issue after so much R&D. what you need to do is just define facebook APPID in string.xml and use that string in meta data in manifest under application tag.

string.xml

<string name="facebook_app_id">XXXXXX</string>

manifest.xml

 <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>
like image 57
Deep Dave Avatar answered Oct 31 '22 12:10

Deep Dave


I finally figured out the issue and came up with the fix, the issue was that the login button could not locate the application app id, you just need to put the application id meta tag inside the application tag element.. Thats all

like image 35
Izuchukwu Oleka Avatar answered Oct 31 '22 13:10

Izuchukwu Oleka