Jawad Rauf Jawad Rauf - 5 months ago 30
Android Question

Cannot resolve symbol callbackManager facebook sdk 4.0's lib

I am working on register my app with facebook login API. Now i get error on callbackManager function. It says cannot resolve symbol callbackManager. Can someone please help me in this regards.

This is my xml.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5b9bd5"
tools:context="com.example.ratingapp.ratingapp.MainActivity">

<ImageButton
android:id="@+id/regiterbutton"
android:layout_width="191dp"
android:layout_height="56dp"
android:background="@drawable/registerbutton"

android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:layout_constraintTop_toTopOf="@+id/activity_main"
app:layout_constraintRight_toRightOf="@+id/loginbutton"
app:layout_constraintBottom_toBottomOf="@+id/activity_main"
app:layout_constraintVertical_bias="0.83" />

<ImageButton
android:id="@+id/loginbutton"
android:layout_width="191dp"
android:layout_height="0dp"
android:background="@drawable/loginbutton"
android:layout_above="@+id/regiterbutton"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
tools:layout_constraintLeft_creator="1"
app:layout_constraintRight_toRightOf="@+id/activity_main"
tools:layout_constraintRight_creator="1"
app:layout_constraintBottom_toTopOf="@+id/regiterbutton"
android:layout_marginBottom="32dp" />
</android.support.constraint.ConstraintLayout>


This is my MainActivity.java

package com.example.ratingapp.ratingapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.accountkit.LoginResult;
import com.facebook.login.LoginManager;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();

LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");

}

@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "Login Cancel",
Toast.LENGTH_LONG).show();
}

@Override
public void onError(FacebookException exception) {
Toast.makeText(MainActivity.this,
exception.getMessage(), Toast.LENGTH_LONG).show();
}
});

setContentView(R.layout.activity_main);

ImageButton registrationbutton = (ImageButton)
findViewById(R.id.regiterbutton);

registrationbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(this,
Arrays.asList("public_profile", "user_friends"));
}
});

}

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

callbackManager.onActivityResult(requestCode, resultCode, data);
}
}

Answer

you are not defining the callBackManager just define it globally like this

public class MainActivity extends AppCompatActivity {
private CallbackManager fbCallback; //define CallbackManager
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
Comments