简体   繁体   English

Android Java:设置从一个活动发送到另一个 Java 类的参数时出错

[英]Android Java: Error in setting parameter sent from one activity to another java class

I have an Activity 'LoginPage' which contains pwd and email.我有一个包含密码和电子邮件的活动“登录页面”。

Once logged in, i am sending the 'email' value as a parameter at some random point to a class 'EmailDetails' in the following manner:-登录后,我将通过以下方式将“电子邮件”值作为某个随机点的参数发送到“电子邮件详细信息”类:-

obj.setEmail(email); //where obj is object of EmailDetails

'EmailDetails' contains the following code:- 'EmailDetails' 包含以下代码:-

public class EmailDetails
{
    public String Usermail;

    public void setEmail(String email)
    {
       Usermail = email; //Username='null' & email='XYZ@yahoo.co.in'
    }

    public String getEmail()
    {
      return Usermail; //Username='null'
    }
}

When some other random activity 'X' wants to access 'email' value in the following manner:-当其他一些随机活动“X”想要通过以下方式访问“电子邮件”值时:-

 email=obj1.getEmail(); //where obj1 is Object of EmailDetails

The 'email' value return null. 'email' 值返回 null。

So, where am i going wrong in the code?那么,我在代码中哪里出错了? Thanks.谢谢。

You have to use intents to send data from one activity to another in android This link might help you http://www.c-sharpcorner.com/uploadfile/kirtan007/passing-data-from-one-activity-to-another-activity-in-android/ 您必须使用意图将数据从一个活动发送到android中的另一个活动。此链接可能会帮助您http://www.c-sharpcorner.com/uploadfile/kirtan007/passing-data-from-one-activity-to-another-在Android活动/

If this will help you accept the answer as true So,it will be helpful for others too. 如果这将帮助您接受正确的答案,那么对其他人也将有所帮助。

Are you storing the data you get from user somewhere? 您是否将从用户那里获得的数据存储在某个地方? I mean does this EmailDetails class persists somewhere outside of the login activity? 我的意思是,此EmailDetails类是否在登录活动之外的某个地方持续存在? try using shared preferences as most simple data persisting storage. 尝试使用共享首选项作为最简单的数据持久存储。 Here's one of most popular answers on this matter on SO How to use SharedPreferences in Android to store, fetch and edit values 这是关于此事的最流行的解答之一,因此如何在Android中使用SharedPreferences来存储,获取和编辑值

Hope this helps! 希望这可以帮助!

The value is null because you used this operator, it should be该值为null因为您使用了this运算符,它应该是

public void setEmail(String email)
    {
       this.Usermail = email; //Username='null' & email='XYZ@yahoo.co.in'
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Java中将值从一类设置为另一类 - Setting value from one class to another in Java How to move from one activity to another ie Java Class to Kotlin Class in an Android Java Project? - How to move from one activity to another i.e Java Class to Kotlin Class in an Android Java Project? 从一个活动更改为另一个活动时的Java绑定错误 - Java Binding error While changing from one Activity to another Activity 在一个类中设置一个值并从Java中的另一个类中检索(Android应用程序) - Setting a value in one class and retrieving from another class in Java (Android app) 调用从另一个活动中调用画布的android java类 - Calling an android java class that calls a canvas from another activity 如何将 android 活动中的数据传递给 Java class? - How to pass data from one android activity to an Java class? 如何在 Java 中将参数从一个传递到另一个 class? - How to pass parameter from one to another class in Java? 如何在Java中将参数从一个类传递到另一个类? - How to pass in the parameter from one class to another in Java? 如何将一种方法从一个 class 迁移到另一个? (安卓、Java) - How to migrate one method from one class to another? (Android, Java) Android将字符串从一项活动发送到另一项无效 - Android sent string from one activity to another not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM