-2
這裏設置TextView的值時,是我SignInActivityCode獲得的NullPointerException從SharedPreferences
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();
}
}
這裏是我正在申請傳遞給在MainActivity我nav_header_main.xml文件中的信息
@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:嘗試在空對象引用
非常感謝!有效 :) –