如何使用android

时间:2017-01-21 09:02:19

标签: android

在我的Android应用程序中,有一个功能使用google plus登录。我成功了。但我希望在下一个活动中显示像用户名,电子邮件在textview中的数据。我已经使用intent put extra标签和检索到下一个活动来完成这些事情。我得到的错误是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);
   }
 }

这是我的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

4 个答案:

答案 0 :(得分:0)

从Intent获取值,因为你已经设置了意图中没有设置包的值,所以你必须得到下面解释的值

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

答案 1 :(得分:0)

您应该安全地检查是否存在具有该键的值以避免nullpointer异常。

像这样修改returnValueFromBundles

private String returnValueFromBundles(String key) {
String returnedValue="";    
if(!getIntent().getExtras().getString(key).equals(null))
{
    returnedValue = getIntent().getExtras().getString(key).toString();
}
return returnedValue;
}

答案 2 :(得分:0)

您正在保存数据,请查看您的代码。 你应该这样使用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");  

答案 3 :(得分:0)

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

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

只需更改上面的代码即可。