2017-02-21 54 views
1
public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener { 

    public SignInButton login; 
    private TextView name; 
    private GoogleApiClient googleApiClient; 
    private GoogleSignInOptions signInOptions; 
    private static final int REQUEST_CODE = 100; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build(); 

     googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, signInOptions).addApi(AppIndex.API).build(); 
     login = (SignInButton) findViewById(R.id.login); 
     name = (TextView) findViewById(R.id.name); 
     login.setSize(SignInButton.SIZE_WIDE); 
     login.setScopes(signInOptions.getScopeArray()); 
     login.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient); 
       startActivityForResult(signInIntent, REQUEST_CODE); 
      } 
     }); 
    } 

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

     if (requestCode == REQUEST_CODE) { 
      GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); 
      GoogleSignInAccount account = result.getSignInAccount(); 

      name.setText(account.getDisplayName()); 
     } 

    } 

    @Override 
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

    } 

    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    public Action getIndexApiAction() { 
     Thing object = new Thing.Builder() 
       .setName("Main Page") // TODO: Define a title for the content shown. 
       // TODO: Make sure this auto-generated URL is correct. 
       .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]")) 
       .build(); 
     return new Action.Builder(Action.TYPE_VIEW) 
       .setObject(object) 
       .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
       .build(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     googleApiClient.connect(); 
     AppIndex.AppIndexApi.start(googleApiClient, getIndexApiAction()); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     AppIndex.AppIndexApi.end(googleApiClient, getIndexApiAction()); 
     googleApiClient.disconnect(); 
    } 

} 

enter image description here顯示java.lang.NullPointerException在與谷歌註冊拋出空

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.SignInButton.setSize(int)' on a null object reference 
        at snapdirectcom.snap.MainActivity.onCreate(MainActivity.java:42) 

厭倦了許多變化,但檢查他們都不曾

+0

您的佈局中是否有名爲login的id? –

+0

似乎'SignInButton login'爲空,最好先檢查你的'activity_login.xml'。 –

+0

'login'爲空。請確保您已使用有效的身份證件 –

回答

0

我注意到你有兩個佈局開放 - 仔細檢查您的評論中的SignInButton位於activity_login中,因爲這是正在搜索的那個,所有其他內容似乎都是按順序排列的

相關問題