这是我的SignInActivityCode
public class SignInActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN=0;
private String TAG = "SignInActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signin_activity);
try{
getSupportActionBar().hide();
}catch (Exception e){
e.printStackTrace();
}
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
signInButton.setSize(SignInButton.SIZE_STANDARD);
signInButton.setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.sign_in_button:
signIn();
break;
}
}
private void signIn(){
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
protected 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);
handleSignInIntent(result);
}
}
private void handleSignInIntent(GoogleSignInResult result){
Log.d(TAG, "handleSignInResult:" + result.isSuccess());
if(result.isSuccess()){
GoogleSignInAccount acct = result.getSignInAccount();
Toast.makeText(this, "Sign in successful!", Toast.LENGTH_LONG).show();
setDefaults(acct.getDisplayName(), acct.getEmail(), acct.getIdToken());
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
}
else{
Toast.makeText(this, "Sign In Failed!\nPlease try again.", Toast.LENGTH_LONG).show();
}
}
public void setDefaults(String name, String email, String token){
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("userName", name);
editor.putString("userEmail", email);
editor.putString("userIdToken", token);
editor.apply();
}
}
以下是我如何在nav_header_main.xml文件中应用传递给MainActivity的信息
@Override
protected void onCreate(Bundle savedInstanceState) {
initializeUserInfo();
}
private void initializeUserInfo() {
userName = (TextView) findViewById(R.id.nav_user_name_tv);
userEmail = (TextView) findViewById(R.id.nav_user_email_tv);
final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
name = sharedPref.getString("userName", "");
email = sharedPref.getString("userEmail", "");
try{
userName.setText(name);
userEmail.setText(email);
}catch(Exception e){
e.printStackTrace();
}
}
以及我在日志中收到的错误
块引用 java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'
答案 0 :(得分:1)
这是初始化和声明导航视图项的方法
<?php
$objForm = new Form();
$objValidation = new Validation($objForm);
$objTalent = new Talent();
$objUpload = new Upload();