Ich habe ein Problem in meinem Projekt gefunden. Ich möchte "Google API" verwenden, um mich in meinem LoginActivity anzumelden. Und Melden Sie sich von einer anderen Aktivität aus (benannt WelcomeActivity)mGoogleApiClient kann nicht in einer anderen Aktivität erreicht werden
LoginActivity: (Code ist here)
public class LoginActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
View.OnClickListener {
// Configuration of Google API - Step 1/3
private static final String TAG = "LoginActivity";
private static final int RC_SIGN_IN = 9001;
public static GoogleApiClient mGoogleApiClient;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GoogleAPI();
}
public void GoogleAPI(){
// Button listeners
findViewById(R.id.sign_in_button).setOnClickListener(this);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
@Override
public void onStart() {
super.onStart();
....
}
// [START onActivityResult]
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}
// [END onActivityResult]
// [START handleSignInResult]
private void handleSignInResult(GoogleSignInResult result) {
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
CustomApplication app = (CustomApplication)getApplication();
GoogleSignInAccount acct = result.getSignInAccount();
Intent i = new Intent(LoginActivity.this, WelcomePage.class);
i.putExtra("Username", acct.getDisplayName());
startActivity(i);
}
}
// [END handleSignInResult]
// [START signIn]
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
// [END signIn]
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
...
}
private void showProgressDialog() {
...
}
private void hideProgressDialog() {
...
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
signIn();
break;
….
}
}
}
Und ich will Sign_out Methode in meiner Willkommen Aktivität verwenden,
private void signOut() {
// Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
// new ResultCallback<Status>() {
// @Override
// public void onResult(Status status) {
// // [START_EXCLUDE]
//// updateUI(false);
// // [END_EXCLUDE]
// }
// });
// }
Um dieses Problem zu lösen, versuche ich 2 Methoden:
machen mGoogleApiClient als eine globale Variable (erweitert Application oder Singleton), ich versuche es, aber fehlgeschlagen, in der Begrüßungsseite, mGoogleApiClient ist nicht null, aber Fehler ist: mGoogleApiClient ist noch nicht verbunden.
i nennen LoginActivity.getMGoogleApiClient (statische Variable), aber auch ausgefallen ist, derselbe Fehler: mGoogleApiClient ist noch nicht verbunden.
Ich suche dieses Problem bereits seit einigen Tagen, aber nichts nützlich, es zu lösen, bitte helfen Sie mir ..
Mögliches Duplikat von [Zugriff google plus client von mehreren Aktivitäten] (http://stackoverflow.com/questions/16827839/access-google-plus-client-from-multiple-activities) – V01D1