2016-04-26 45 views
-1

對不起我的英語!需要調用另一個活動自動的,更不知道往哪裏放碼,「Facebook登錄SDK」是怎麼回事代碼,「startActivity」不起作用我需要自動調用另一個活動,更多不知道在哪裏放置代碼,Facebook登錄

/** * A placeholder fragment containing a simple view. */ 
public class MainFragment extends Fragment { 

private TextView mTextDetails; 
private CallbackManager mCallbackManager; 
private AccessTokenTracker mTokenTracker; 
private ProfileTracker mProfileTracker; 

private FacebookCallback<LoginResult> mFacebookCallback = new FacebookCallback<LoginResult>() { 
    @Override 
    public void onSuccess(LoginResult loginResult) { 
     Log.d("KeyHash", "onSuccess"); 
     AccessToken accessToken = loginResult.getAccessToken(); 
     Profile profile = Profile.getCurrentProfile(); 
     mTextDetails.setText(constructWelcomeMessage(profile)); 

    } 


    @Override 
    public void onCancel() { 
     Log.d("KeyHash", "onCancel"); 
    } 

    @Override 
    public void onError(FacebookException e) { 
     Log.d("KeyHash", "onError " + e); 
    } 
}; 


public MainFragment() { 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext()); 

    mCallbackManager = CallbackManager.Factory.create(); 
    setupTokenTracker(); 
    setupProfileTracker(); 

    mTokenTracker.startTracking(); 
    mProfileTracker.startTracking(); 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.fragment_main, container, false); 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    setupTextDetails(view); 
    setupLoginButton(view); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    Profile profile = Profile.getCurrentProfile(); 
    mTextDetails.setText(constructWelcomeMessage(profile)); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    mTokenTracker.stopTracking(); 
    mProfileTracker.stopTracking(); 
} 

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

private void setupTextDetails(View view) { 
    mTextDetails = (TextView) view.findViewById(R.id.text_details); 
} 

private void setupTokenTracker() { 
    mTokenTracker = new AccessTokenTracker() { 
     @Override 
     protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken currentAccessToken) { 
      Log.d("KeyHash", "Welcome" + currentAccessToken); 
     } 
    }; 
} 

private void setupProfileTracker() { 
    mProfileTracker = new ProfileTracker() { 
     @Override 
     protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) { 
      Log.d("KeyHash", "Welcome" + currentProfile); 
      mTextDetails.setText(constructWelcomeMessage(currentProfile)); 
     } 
    }; 
} 

private void setupLoginButton(View view) { 
    LoginButton mButtonLogin = (LoginButton) view.findViewById(R.id.login_button); 
    mButtonLogin.setFragment(this); 
    mButtonLogin.setReadPermissions("public_profile,user_friends,email"); 
    mButtonLogin.registerCallback(mCallbackManager, mFacebookCallback); 
} 

private String constructWelcomeMessage(Profile profile) { 
    StringBuffer stringBuffer = new StringBuffer(); 
    if (profile != null) { 
     stringBuffer.append("Welcome " + profile.getName()); 
    } 
    return stringBuffer.toString(); 
} 

} 
+0

試試這個裏面你如果(個人資料!= NULL)你可以調用其他活動,並且可以在它 –

+0

獲得用戶的詳細信息,繼續錯誤:( –

回答

1

如果你想盡快用戶成功開始活動登錄後,您可以撥打startActivity(intent)中的FacebookCallback的onSuccess(LoginResult loginResult)方法。

+0

我使用的代碼,startActivity (New Intent(MainFragment.this,HomeActivity.class)); - 更多繼續錯誤 –

1

中的onSuccess方法

GraphRequest request = GraphRequest.newMeRequest(
         accessToken, 
         new GraphRequest.GraphJSONObjectCallback() { 
          @Override 
          public void onCompleted(
            JSONObject object, 
            GraphResponse response) { 
           // Application code 
           try { 

            String name = object.getString("name"); 
            String id = object.getString("id"); 

            Intent redirect=new Intent(Login_Activity.this,anotheractivity.class); 
            startActivity(redirect); 

           } catch (JSONException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
           } 
          } 
         }); 
+0

「錯誤:(63,49)錯誤:找不到合適的構造函數Intent(MainFragment,Class ) 構造函數Intent.Intent字符串,Uri)不適用 (參數不匹配; MainFragment不能轉換爲字符串) 構造函數Intent.Intent(Context,Clas s )不適用 (參數不匹配; MainFragment無法轉換爲上下文)「 –

+0

檢查此[鏈接](http://stackoverflow.com/questions/27704006/error-using-onclicklistener-intent) – Rgv

相關問題