2017-01-21 21 views
0

私のアンドロイドアプリには、Google Plusでログインする機能があります。私はうまくいった。しかし、私は次の活動のテキストビューでユーザー名、電子メールのようなそのデータを表示したい。私はすでにこれらのことを意図して、余分なタグを入れて、次の活動に取り込んでいます。バンドルのヌル参照であるエラーが発生しました。ここで私はエラーログを添付しました。前もって感謝します。ここ androidのバンドルオブジェクトを使用してデータを取得する方法

Googleplus.java 
private void handleSignInResult(GoogleSignInResult result) { 
    Log.d(TAG, "handleSignInResult:" + result.isSuccess()); 
    if (result.isSuccess()) { 
     // Signed in successfully, show authenticated UI. 
     final GoogleSignInAccount acct = result.getSignInAccount(); 

     Log.e(TAG, "display name: " + acct.getGivenName()); 

     String personName = acct.getGivenName(); 
     String personLastName = acct.getFamilyName(); 
     String email = acct.getEmail(); 

     Log.e(TAG, "Name: " + personName + ", email: " + email + ", lastname: " + personLastName); 

     loginButton.setVisibility(View.GONE); 
     btnSignIn.setVisibility(View.GONE); 
     txtDisplayText.setVisibility(View.GONE); 
     btnLogin.setVisibility(View.GONE); 
     app_bar.setVisibility(View.GONE); 
     txtFooterText.setVisibility(View.GONE); 
     digitsButton.setVisibility(View.GONE); 
     activity_choose_login_account.setBackgroundResource(R.mipmap.scree); 
     Intent ii = new Intent(ChooseLoginAccountActivity.this, GoogleplusActivity.class); 
     ii.putExtra(PROFILE_USERNAME, acct.getGivenName()); 
     ii.putExtra(PROFILE_USERLASTNAME, acct.getFamilyName()); 
     ii.putExtra(PROFILE_EMAIL_GOOGLE, acct.getEmail()); 
     startActivity(ii); 
    } 
} 

は私のretriveuserinfo.java

create method for retrieve bundle 

private String returnValueFromBundles(String key) { 
    Bundle inBundle = getIntent().getExtras(); 
    String returnedValue = inBundle.get(key).toString(); 
    return returnedValue; 
} 

//here get value from chooseloginactivity.java 
String profilename = returnValueFromBundles(ChooseLoginAccountActivity.PROFILE_USERNAME); 
    String profilelastname = returnValueFromBundles(ChooseLoginAccountActivity.PROFILE_USERLASTNAME); 
    String profileemail = returnValueFromBundles(ChooseLoginAccountActivity.PROFILE_EMAIL_GOOGLE); 

    //textview findviewbyid 
    txtFirstName =(TextView)customView.findViewById(R.id.txtFirstName); 
    txtLastName = (TextView) customView.findViewById(R.id.txtLastName); 
    txtEmail = (TextView) customView.findViewById(R.id.txtEmail); 

    //set bundle value in textview for displaying 
    txtFirstName.setText(profilename); 
    txtLastName.setText(profilelastname); 
    txtEmail.setText(profileemail); 

ですが、私はエラーnull object reference

答えて

0

あなたがそうバンドルを設定していない意思に値を設定しているためのような意図から値を取得しました以下に説明する値を取得する必要があります

String profilename=getIntent().getStringExtra((PROFILE_USERNAME); 
String profle=getIntent().getStringExtra((PROFILE_USERLASTNAME); 
+0

ご協力いただきありがとうございます。 :) @ user2285117 –

+0

あなたの歓迎ハッピーコーディング... !! –

0

nullキーの例外を回避するためにそのキーを持つ値がある場合

ので、あなたのコードを見てあなたが意図にデータを保存しているあなたのreturnValueFromBundles

このような
private String returnValueFromBundles(String key) { 
String returnedValue="";  
if(!getIntent().getExtras().getString(key).equals(null)) 
{ 
    returnedValue = getIntent().getExtras().getString(key).toString(); 
} 
return returnedValue; 
} 
0

を変更します。 この方法でバンドルを使用する必要があります。チェックしてコードを編集してください。 2つ目の活動で

Intent i = new Intent(this, SecondActivity.class); 

//Create the bundle 
Bundle bundle = new Bundle(); 
//Add your data from getFactualResults method to bundle 
bundle.putString("KEY", value); 
//Add the bundle to the intent 
i.putExtras(bundle); 

//Fire the second activity 
startActivity(i); 

}

Bundle bundle = getIntent().getExtras(); 

//Extract the data… 
String value = bundle.getString("KEY"); 
0

あなたは上記のようにコードを変更するには、この

Intent ii = new Intent(ChooseLoginAccountActivity.this, GoogleplusActivity.class); 
    Bundle b = new Bundle(); 
    b.putString(PROFILE_USERNAME, acct.getGivenName()); 
    b.putString(PROFILE_USERLASTNAME, acct.getFamilyName()); 
    b.putString(PROFILE_EMAIL_GOOGLE, acct.getEmail()); 
    ii.putExtras(b); 
    startActivity(ii); 

のようなバンドルに自分の価値観を置くことができます。

関連する問題