问题
I got this code from Github.I followed all the directions to the later and there is an error in the MainActivity
which i can't seem to resolve.
Here is the code:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.facebook.LoggingBehavior;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.Settings;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupFacebookConnect(savedInstanceState);
Button facebook_connect = (Button) findViewById(R.id.facebookLoginButton);
facebook_connect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isFacebookConnected()) {
disConnectFacebook();
} else {
connectFacebook();
}
}
});
}
private Session.StatusCallback statusCallback = new FBSessionStatus();
public void setupFacebookConnect(Bundle savedInstanceState) {
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback,
savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this)
.setCallback(statusCallback));
}
}
}
public boolean isFacebookConnected() {
Session session = Session.getActiveSession();
return (session.isOpened()) ? true : false;
}
public void connectFacebook() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this)
.setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
}
@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}
public class FBSessionStatus implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state,
Exception exception) {
}
}
public void disConnectFacebook() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
}
and here is the part where the error is:
Just incase you can't read it, it says
The type Android.support.v4.app.Fragment cannot be resolved.It is indirectly refrenced from required .class files
What am i doing wrong?
回答1:
You must add the Android v4 support library to your project. Take a look here to see how to setup.
来源:https://stackoverflow.com/questions/20600929/error-connecting-to-android-app-through-facebook-session