2014-01-10 54 views
0

我想將Google +登錄集成到我的Android應用程序中。我在Android清單文件中賦予了權限,並初始化了plusclient對象,但是當使用登錄按鈕時,我收到一個錯誤「不幸的是,應用程序已經停止!」。Android應用程序中的Plusclient.connect()不起作用

Android清單文件是:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.abs" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" /> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/> 
    <uses-permission android:name="android.permission.USE_CREDENTIALS"/> 


    <application 
     android:allowBackup="true" 
     android:icon="@drawable/abs_icon" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.abs.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.abs.Bank" 
      android:label="@string/title_activity_bank" > 
     </activity> 
     <activity 
      android:name="com.abs.Scheme" 
      android:label="@string/title_activity_scheme" > 
     </activity> 
     <activity 
      android:name="com.abs.Login" 
      android:label="@string/title_activity_login" > 

     </activity> 
    </application> 

</manifest> 

Mainactivity是:

package com.abs; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 


public class MainActivity extends Activity { 


    Button bank; 
    Button scheme; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 


    bank = (Button) findViewById(R.id.button1); 
    scheme = (Button) findViewById(R.id.button2); 


    } 



    public void onClick_bank(View v){ 
     Toast.makeText(MainActivity.this, "Searching by bank", Toast.LENGTH_SHORT).show(); 

     Intent ibank = new Intent(v.getContext(),Bank.class); 
     startActivityForResult(ibank, 0); 

    } 

    public void onClick_scheme(View v){ 
     Toast.makeText(MainActivity.this, "Searching by scheme", Toast.LENGTH_SHORT).show(); 

     Intent ischeme = new Intent(v.getContext(),Scheme.class); 
     startActivityForResult(ischeme, 0); 
    } 

    public void onClick_login(View v){ 

     Toast.makeText(MainActivity.this, "Please login with gmail id", Toast.LENGTH_SHORT).show(); 
     Intent islog = new Intent(v.getContext(),Login.class); 
     startActivityForResult(islog, 0); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 




} 

爲谷歌+登錄登錄活動下面給出,我已經採取的代碼從谷歌的例子:用於切換到登錄。從mainactivity我不得不評論onstart(),因爲mplusclient.connect()不工作,請幫助...

package com.abs; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.common.Scopes; 
import com.google.android.gms.common.SignInButton; 
import com.google.android.gms.plus.PlusClient; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.IntentSender; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Login extends Activity implements OnClickListener, 
     PlusClient.ConnectionCallbacks, PlusClient.OnConnectionFailedListener, 
     PlusClient.OnAccessRevokedListener { 

    private static final int DIALOG_GET_GOOGLE_PLAY_SERVICES = 1; 

    private static final int REQUEST_CODE_SIGN_IN = 1; 
    private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 2; 

    private TextView mSignInStatus; 
    private PlusClient mPlusClient; 
    private SignInButton mSignInButton; 
    private View mSignOutButton; 
    private View mRevokeAccessButton; 
    private ConnectionResult mConnectionResult; 
    private ProgressDialog mConnectionProgressDialog; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     mPlusClient = new PlusClient.Builder(Login.this, Login.this, Login.this) 
       .setActions("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity") 
       .setScopes(Scopes.PLUS_LOGIN) 
       .build(); 
     mConnectionProgressDialog = new ProgressDialog(this); 
     mConnectionProgressDialog.setMessage("Signing in..."); 

     mSignInStatus = (TextView) findViewById(R.id.sign_in_status); 
     mSignInButton = (SignInButton) findViewById(R.id.sign_in_button); 
     mSignInButton.setOnClickListener(this); 
     mSignOutButton = findViewById(R.id.sign_out_button); 
     mSignOutButton.setOnClickListener(this); 
     mRevokeAccessButton = findViewById(R.id.revoke_access_button); 
     mRevokeAccessButton.setOnClickListener(this); 
    } 
    /* 
    @Override 
    public void onStart() { 
     super.onStart(); 
     mPlusClient.connect(); 
    } 

    @Override 
    public void onStop() { 
     mPlusClient.disconnect(); 
     super.onStop(); 
    } 
*/ 
    @SuppressWarnings("deprecation") 
    @Override 
    public void onClick(View view) { 

     switch(view.getId()) { 
     case R.id.sign_in_button: 
      if(!mPlusClient.isConnected()){ 

       mPlusClient.connect(); 
      } 
      //mPlusClient.connect(); 

      /* 
      int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(Login.this); 
        if (available == ConnectionResult.SUCCESS) { 
         showDialog(DIALOG_GET_GOOGLE_PLAY_SERVICES); 
        return; 
       } 

       try { 
        mSignInStatus.setText("Signing in"); 
        mConnectionResult.startResolutionForResult(Login.this, REQUEST_CODE_SIGN_IN); 
       } 
       catch (IntentSender.SendIntentException e) { 
        // Fetch a new result to start. 
        mPlusClient.connect(); 
       } 
      */ 
      break; 
     case R.id.sign_out_button: 

      if (mPlusClient.isConnected()) { 
       mPlusClient.clearDefaultAccount(); 
       mPlusClient.disconnect(); 
       mPlusClient.connect(); 
      } 
      else{Toast.makeText(Login.this, "You are not connected to internet", Toast.LENGTH_LONG).show();} 
      break; 
     case R.id.revoke_access_button: 

      if (mPlusClient.isConnected()) { 
       mPlusClient.revokeAccessAndDisconnect(this); 
       updateButtons(false /* isSignedIn */); 
      } 
      else{Toast.makeText(Login.this, "You are not connected to internet", Toast.LENGTH_LONG).show();} 
      break; 
    } 

    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     if (id != DIALOG_GET_GOOGLE_PLAY_SERVICES) { 
      return super.onCreateDialog(id); 
     } 

     int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     if (available == ConnectionResult.SUCCESS) { 
      return null; 
     } 
     if (GooglePlayServicesUtil.isUserRecoverableError(available)) { 
      return GooglePlayServicesUtil.getErrorDialog(
        available, Login.this, REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES); 
     } 
     return new AlertDialog.Builder(this) 
       .setMessage("+ generic error") 
       .setCancelable(true) 
       .create(); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     Toast.makeText(Login.this, "activity result disabled", Toast.LENGTH_LONG).show(); 
     if (requestCode == REQUEST_CODE_SIGN_IN 
       || requestCode == REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES) { 
      if (resultCode == RESULT_OK && !mPlusClient.isConnected() 
        && !mPlusClient.isConnecting()) { 
       // This time, connect should succeed. 
       mPlusClient.connect(); 
      } 
     } 
    } 

    @Override 
    public void onAccessRevoked(ConnectionResult status) { 
     Toast.makeText(Login.this, "Access revoke not available", Toast.LENGTH_LONG).show(); 
     if (status.isSuccess()) { 
      mSignInStatus.setText("revoke access status"); 
     } else { 
      mSignInStatus.setText("revoke access status error"); 
      mPlusClient.disconnect(); 
     } 
     mPlusClient.connect(); 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     Toast.makeText(Login.this, "Client is connected", Toast.LENGTH_LONG).show(); 
      String currentPersonName = mPlusClient.getCurrentPerson() != null 
       ? mPlusClient.getCurrentPerson().getDisplayName() 
       : "Signed in currentPersonName"; 
     mSignInStatus.setText("signed in as you"); 
     updateButtons(true);/* isSignedIn */ 
    } 

    @Override 
    public void onDisconnected() { 
     Toast.makeText(Login.this, "Client is disconnected", Toast.LENGTH_LONG).show(); 
      mSignInStatus.setText("Loading status"); 
     mPlusClient.connect(); 
     updateButtons(false);/* isSignedIn */ 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     Toast.makeText(Login.this, "Connection failed", Toast.LENGTH_LONG).show(); 
      mConnectionResult = result; 
     updateButtons(false);/* isSignedIn */ 
    } 

    private void updateButtons(boolean isSignedIn) { 
     if (isSignedIn) { 
      mSignInButton.setVisibility(View.INVISIBLE); 
      mSignOutButton.setEnabled(true); 
      mRevokeAccessButton.setEnabled(true); 
     } else { 
      if (mConnectionResult == null) { 
       // Disable the sign-in button until onConnectionFailed is called with result. 
       mSignInButton.setVisibility(View.INVISIBLE); 
       mSignInStatus.setText("Loading status"); 
      } else { 
       // Enable the sign-in button since a connection result is available. 
       mSignInButton.setVisibility(View.VISIBLE); 
       mSignInStatus.setText("Signed out"); 
      } 

      mSignOutButton.setEnabled(false); 
      mRevokeAccessButton.setEnabled(false); 
     } 
    } 
} 

謝謝!

+1

哪裏是烏爾logcat的痕跡? – keshav

回答

1

我一直在搞相同的代碼,並最終讓它運行一些修改。

根據我的說法,mPlusClient.connect();必須在點擊之前調用,因爲它連接異步,如果在onclick中使用它會給出錯誤。

,這裏是我的onclick()函數

@Override 
public void onClick(View arg0) { 

    int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (available != ConnectionResult.SUCCESS) { 
     showGoogleDialog(); 
     return; 
    } 
    try { 
     mConnectionResult.startResolutionForResult(this, REQUEST_CODE_SIGN_IN); 
    } catch (IntentSender.SendIntentException e) { 
     mPlusClient.connect(); 
    } catch (NullPointerException e1) { 
     PvrLog.d("null pointer exception"); 
    } 
} 
相關問題