2016-05-07 9 views
0

Ich versuche, die folgende Google-Login zu implementieren und ich erhalte den folgenden Fehler:Erste Fehler zum Erstellen von Google Anmeldung

public class login extends AppCompatActivity implements 
     GoogleApiClient.OnConnectionFailedListener, 
     View.OnClickListener { 

    private LoginButton loginButton; 
    private CallbackManager callbackManager; 
    private Button mButton; 
    private TextView mTextDetail; 
    private TextView mTextAccessToken; 
    private GoogleApiClient mGoogleApiClient; 
    private static final String TAG = "SignInActivity"; 
    private static final int RC_SIGN_IN = 9001; 

    private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult loginResult) { 

//   mTextDetail = (TextView) findViewById(R.id.editText); 
//   mTextAccessToken = (TextView) findViewById(R.id.editText2); 

      AccessToken accessToken = loginResult.getAccessToken(); 
      Profile profile = Profile.getCurrentProfile(); 

      if(profile != null){ 
//    mTextDetail.setText(profile.getFirstName()); 
//    mTextAccessToken.setText(profile.getLastName()); 

       Intent intent = new Intent(login.this,Home.class); 
       startActivity(intent); 

      } 

     } 

     @Override 
     public void onCancel() { 

     } 

     @Override 
     public void onError(FacebookException error) { 

     } 
    }; 





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

     Log.i("Looks like it work","Looks like it work"); 
     findViewById(R.id.sign_in_button).setOnClickListener(this); 

     findViewById(R.id.sign_in_button).setOnClickListener(this); 
     //findViewById(R.id.sign_out_button).setOnClickListener(this); 
     //findViewById(R.id.disconnect_button).setOnClickListener(this); 

     // [START configure_signin] 
     // Configure sign-in to request the user's ID, email address, and basic 
     // profile. ID and basic profile are included in DEFAULT_SIGN_IN. 
     GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) 
       .requestEmail() 
       .build(); 
     // [END configure_signin] 

     // [START build_client] 
     // Build a GoogleApiClient with access to the Google Sign-In API and the 
     // options specified by gso. 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */) 
       .addApi(Auth.GOOGLE_SIGN_IN_API, gso) 
       .build(); 
     // [END build_client] 

     // [START customize_button] 
     // Customize sign-in button. The sign-in button can be displayed in 
     // multiple sizes and color schemes. It can also be contextually 
     // rendered based on the requested scopes. For example. a red button may 
     // be displayed when Google+ scopes are requested, but a white button 
     // may be displayed when only basic profile is requested. Try adding the 
     // Scopes.PLUS_LOGIN scope to the GoogleSignInOptions to see the 
     // difference. 
     SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); 
     signInButton.setSize(SignInButton.SIZE_STANDARD); 
     signInButton.setScopes(gso.getScopeArray()); 
     // [END customize_button] 







     FacebookSdk.sdkInitialize(getApplicationContext()); 
     AppEventsLogger.activateApp(this); 
     setContentView(R.layout.activity_login); 

     mButton = (Button) findViewById(R.id.but_goToInterest); 

     mButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(login.this, InterestScreen2.class); 
       startActivity(intent); 
//    Intent intent = new Intent(login.this,Home.class); 
//    startActivity(intent); 
      } 
     }); 

     //Hide the Toolbar 
     getSupportActionBar().hide(); 

     //Initialize Facebook SDK 



     loginButton = (LoginButton) findViewById(R.id.login_button); 
     loginButton.setReadPermissions("user_friends"); 
     callbackManager = CallbackManager.Factory.create(); 

     loginButton.registerCallback(callbackManager,mCallback); 



    } 

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

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 

    private void signIn() { 
     Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); 
     startActivityForResult(signInIntent, RC_SIGN_IN); 
    } 


    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.sign_in_button: 
       signIn(); 
       break; 
      //case R.id.sign_out_button: 
       // signOut(); 
       // break; 
      //case R.id.disconnect_button: 
       // revokeAccess(); 
       // break; 
     } 
    } 
} 

Der Fehler ist:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Was mache ich falsch hier tun ?

Antwort

1

Nach dieser Aussage

signInButton.setScopes(gso.getScopeArray()); 

legte einen Klick-Listener für signInButton

signInButton.setOnClickListener(this); 

und nach dieser Aussage

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

setzen diese ein

setContentView(R.layout.activity_login); 

entfernen Sie diese Anweisungen auch.

findViewById(R.id.sign_in_button).setOnClickListener(this); 
+0

noch immer den gleichen Fehler @Stallion – Nant

+0

@Nant – Stallion

+0

meine bearbeitet Antwort überprüfen Ja, das funktioniert jetzt. Ich habe die Inhaltsansicht nach dem Einstellen der Schaltfläche eingestellt. Vielen Dank @Stallion – Nant

Verwandte Themen