In my Android project I have 3 classes:
1st a List class contains a login button and 2 EditText boxes for user name and password respectively
Code looks like this :
signin.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
EditText etxt_user = (EditText) findViewById(R.id.usereditlog);
t_id = etxt_user.getText().toString();
EditText etxt_password = (EditText) findViewById(R.id.pwdeditlog);
password = etxt_password.getText().toString();
}
})
2nd class is App which extends Application
class App extends Application
{
List session = new List();
public String getUsername()
{
return session.t_id;
}
public void setUsername(String username)
{
session.t_id = username;
}
public String getPassword()
{
return session.password;
}
public void setPassword(String password)
{
session.password = password;
}
}
3rd class is Utils where in I am facing problem in retrieving the user name and password entered during the initial execution of the List class
public class Utils extends Activity
{
public static List mySessionObject = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button signin = (Button) findViewById(R.id.regsubmitbtn);
signin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Utils.mySessionObject = new List();
//some extra initalization, for example setting userId
App app = (App) getApplication();
String username = app.getUsername();
String password= app.getPassword();
}
});
}
}
The problem I am facing is retrieving the username and password from my 3rd class. For some reason it’s not working properly.
You need to specify the name of your application class in manifest file like as below
and initializing variable like below….
and retrive using following line,