Error connecting to Android app through Facebook Session

非 Y 不嫁゛ 提交于 2020-01-03 04:38:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!