如何在android中使用bundle对象检索数据

如何在android中使用bundle对象检索数据,android,Android,在我的android应用程序中,有一个使用google plus登录的功能。我做得很成功。但我想在下一个活动中在文本视图中显示用户名、电子邮件等数据。我已经使用intent完成了这些事情,将额外的标记和检索放入下一个活动中。我得到的错误是bundle null引用。这里我附加了错误日志。提前谢谢 Googleplus.java private void handleSignInResult(GoogleSignInResult result) { Log.d(TAG, "handle

在我的android应用程序中,有一个使用google plus登录的功能。我做得很成功。但我想在下一个活动中在文本视图中显示用户名、电子邮件等数据。我已经使用intent完成了这些事情,将额外的标记和检索放入下一个活动中。我得到的错误是bundle null引用。这里我附加了错误日志。提前谢谢

 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);
   }
 }
这是我的retrieveuserinfo.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);

但是我得到了错误

从Intent中获取值,因为您在Intent中设置了值,但您没有设置bundle,所以您必须获取下面解释的值

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

您应该安全地检查该键是否有值,以避免nullpointer异常

像这样修改您的
returnvaluefrombandles

private String returnValueFromBundles(String key) {
String returnedValue="";    
if(!getIntent().getExtras().getString(key).equals(null))
{
    returnedValue = getIntent().getExtras().getString(key).toString();
}
return returnedValue;
}
    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);

您正在意向性地保存数据,因此请查看您的代码。 你应该这样使用bundle。。检查并编辑代码

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");  

您可以像这样将您的值放入Bundle中

private String returnValueFromBundles(String key) {
String returnedValue="";    
if(!getIntent().getExtras().getString(key).equals(null))
{
    returnedValue = getIntent().getExtras().getString(key).toString();
}
return returnedValue;
}
    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);
只需像上面那样更改代码