Try invoking the virtual method 'int java.lang.Object.hashCode ()' on a null object reference

advertisements

I'm trying to implement Facebook login button, but i'm receiving these errors when trying to run app:

Error:

FATAL EXCEPTION: AsyncTask #4
Process: , PID: 4633
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
    at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
    at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
    at com.facebook.internal.Utility.queryAppSettings(Utility.java:825)
    at com.facebook.login.widget.LoginButton$1.run(LoginButton.java:489)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
com.android.vlada.sportisa E/GoogleApiAvailability: Google Play services is invalid. Cannot recover.

Code:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {

    private Button buttonRegistrujSe, buttonPrijaviSeKaoGost, buttonPrijavaRegistrovaniKorisnik;
    private LoginButton loginButton;
    private CallbackManager callbackManager;
    private TextView info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FacebookSdk.sdkInitialize(getApplicationContext());

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        GoogleApiClient mGoogleApiClient;
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        buttonRegistrujSe = (Button) (findViewById(R.id.button_registruj_se));
        buttonRegistrujSe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, RegistrationActivity.class);
                startActivity(i);
            }
        });

        buttonPrijaviSeKaoGost = (Button) (findViewById(R.id.button_prijavi_se_kao_gost));
        buttonPrijaviSeKaoGost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i2 = new Intent(MainActivity.this, LoginGostActivity.class);
                startActivity(i2);
            }
        });

        buttonPrijavaRegistrovaniKorisnik = (Button) (findViewById(R.id.button_prijava_registrovani_korisnik));
        buttonPrijavaRegistrovaniKorisnik.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i3 = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(i3);
            }
        });

        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);
        info = (TextView)findViewById(R.id.info);
        loginButton = (LoginButton)findViewById(R.id.connectWithFbButton);

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                info.setText(
                        "User ID: "
                                + loginResult.getAccessToken().getUserId()
                                + "\n" +
                                "Auth Token: "
                                + loginResult.getAccessToken().getToken()
                );
            }

            @Override
            public void onCancel() {
                info.setText("Login attempt canceled.");
            }

            @Override
            public void onError(FacebookException e) {
                info.setText("Login attempt failed.");
            }
        });
    }    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }    

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.exit(0);
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }
}

Layout Code

<com.facebook.login.widget.LoginButton
    android:id="@+id/connectWithFbButton"
    style="@style/com_facebook_loginview_default_style"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_above="@+id/sign_in_button"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="43dp" />

Update: after i deleted duplicate code in MainActivity i'm still receiving the same errors when trying to ran app. I'm attaching Manifest code also as requested so maybe that would help? Thanx

<?xml version="1.0" encoding="utf-8"?>

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />

<permission
    android:name="com.android.vlada.sportisa.Entiteti.maps.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="com.android.vlada.sportisa.Entiteti.maps.permission.MAPS_RECEIVE" />

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyAVb2JuN-5nbaVkGODivuXPJOF5OeRekzo" />
<meta-data
    android:name="com.facebook.sdk.ApplicationId"
    android:value="@string/facebook_login" />

<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" />
    <activity android:name=".SplashActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".RegistrationActivity" />
    <activity android:name=".LoginActivity" />
    <activity android:name=".BiranjeAktivnosti" />
    <activity
        android:name=".Trazim"
        android:label="@string/title_activity_trazim"
        android:theme="@style/AppTheme" />
    <activity
        android:name=".Organizujem"
        android:label="@string/title_activity_organizujem"
        android:theme="@style/AppTheme" />
    <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" />
</application>


Add metadata inside application tag before activities. What you have now is:

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyAVb2JuN-5nbaVkGODivuXPJOF5OeRekzo" />
<meta-data
    android:name="com.facebook.sdk.ApplicationId"
    android:value="@string/facebook_login" />

<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" />
    <activity android:name=".SplashActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    ...
</application>

Move metadata inside application tag like this:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyAVb2JuN-5nbaVkGODivuXPJOF5OeRekzo" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_login" />

    <activity android:name=".MainActivity" />
    <activity android:name=".SplashActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    ...
</application>