フェイスブックの統合で成功したログイン試行から新しいアクティビティを開始しようとしましたが、ログインボタンが押された後にログインボタンが押されたときにログインボタンと同じアクティビティに戻ります。Facebookのログイン成功後に新たなアクティビティを開始
私はすでに次のアクティビティに移行する意思を持っていますが、ログインの成功に対する応答はないようです。ここで
は(私は今、長い間これをしてきた乱雑して申し訳ありません)私のコードです:import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.util.Log;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphResponse;
import com.facebook.login.Login;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import android.widget.Toast;
import com.facebook.GraphRequest;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import org.json.JSONObject;
public class userlogin extends FragmentActivity {
LoginButton loginButton; //The Facebook login button
TextView loginstatus;
CallbackManager callbackManager;
private AccessToken accessToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_userlogin);
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
loginstatus = (TextView)findViewById(R.id.loginstatus);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
// **Description: If the user cancels the login before it is completed a message will
// be shown to let them know that they are not logged in
//**Parameters:
//** Loginresult: the results of the login attempt.
//**
//**Precondition: loginButton must have been clicked first
//**
//**throws: N/A
//** returns: a textView that displays a message of whether or not the login was succesful
public void onSuccess(LoginResult loginResult) {
loginstatus.setText("Login Success");
accessToken = loginResult.getAccessToken();
Toast.makeText(userlogin.this, "Eureka", Toast.LENGTH_LONG).show();
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("FB", "complete");
Log.d("FB", object.optString("name"));
Log.d("FB", object.optString("link"));
Log.d("FB", object.optString("id"));
Log.d("FB", object.optString("gender"));
TextView Username;
Username = (TextView) findViewById(R.id.loginstatus);
String id = object.optString("id");
String name = object.optString("name");
Username.setText("Welcome:" + name);
ImageView userPhoto = (ImageView) findViewById(R.id.facebookuserPortrait);
String i = "http://graph.facebook.com/" + id + "/picture?type=large";
Glide.with(userlogin.this).load(i).override(350, 350).into(userPhoto);
Intent intent = new Intent(userlogin.this, AlertsandPlanning.class);
startActivity(intent);
}
// **Description: If the user cancels the login before it is completed a message will
// be shown to let them know that they are not logged in
//**Parameters: N/A
//**
//**Precondition: loginButton must have been clicked first
//**
//**throws: N/A
//** returns: a textView that displays a message
public void onCancel() {
loginstatus.setText("Login Canceled");
}
// **Description: If the user has an error during the login before it is completed
// a message will be shown to let them know that they are not logged in
//**Parameters: FacebookException: an error transpoding if the login fails
//**
//**Precondition: loginButton must have been clicked first
//**
//**throws: Exception
//** returns: a textView that displays a message
public void onError(FacebookException exception) {
loginstatus.setText("Something went wrong \n+" +
"Check your credentials and try again");
}
});
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
protected void onActivivityResult(int requestCode, int resultCode, Intent data) {
userlogin.super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
});
}
}私は与えられた任意の洞察力に感謝
、私は上のゼロから始めることができます何も動作しない場合は、このアクティビティを再試行してください。
問題を再現する:「フェイスブックで続ける」ボタンがクリックされ、次に表示されるアクティビティはAlertsandPlanningアクティビティではなく、userloginアクティビティです。
更新:
私は、次のコードを使用してみてください、ここでは現在の最初のコメントが更新されたコードである
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import com.facebook.CallbackManager;
import com.facebook.login.widget.LoginButton;
import com.facebook.FacebookSdk;
import android.view.View;
import com.facebook.login.LoginResult;
import com.facebook.FacebookCallback;
import org.json.JSONObject;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.login.LoginManager;
import com.facebook.GraphResponse;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphRequestAsyncTask;
public class UserLogin extends AppCompatActivity implements View.OnClickListener {
LoginButton btnFacebookLogin;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_user_login);
callbackManager = CallbackManager.Factory.create();
setUI();
}
private void setUI() {
btnFacebookLogin = (LoginButton) findViewById(R.id.FBLoginBtn);
btnFacebookLogin.setOnClickListener(this);
btnFacebookLogin.setReadPermissions("email", "public_profile");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.FBLoginBtn:
facebookLogin();
}
}
private void facebookLogin() {
// Callback registration
btnFacebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//アプリケーションコード
final AccessToken accessToken = loginResult.getAccessToken();
GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
String username = (user.optString("name"));
}
}).executeAsync();
Toast.makeText(getApplicationContext(), "Login Success with facebook", Toast.LENGTH_SHORT).show();
Intent startAlertsAndPlanning = new Intent(UserLogin.this, AlertsAndPlanning.class);
startActivity(startAlertsAndPlanning);
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
問題が解決したら、問題を解決して適切に分析するのに役立つログをここに投稿してください。 –
上記のコードでエラーは発生しませんでしたが、投稿する前にいくつかのエラーがありましたが、分析の際に修正されました。 – thebluechemist