Reputation: 2088
MainActivity.java :
Intent intent = new Intent(MainActivity.this,Profile_view.class);
intent.putExtra("user",username);
startActivity(intent);
Profil_view :
Intent intent = this.getIntent();
if(intent!=null)
{
String user = intent.getExtras().getString("user");
Log.i("ProfileView",user);
DBAdapter dbAdapter = new DBAdapter(Profile_view.this);
dbAdapter.open();
String name = dbAdapter.getName(user);
full_name.setText(name);
username_display.setText(dbAdapter.getUserName(user));
mail_display.setText(dbAdapter.getMail_id(user));
date_of_birth_display.setText(dbAdapter.get_date_of_birth(user));
//country_display.setText(dbAdapter.get_country(user));
dbAdapter.close();
}
I have a situation here at which i register the user details and move to profile_view
page to display it and similarly when i login at the main activity, it matches with the current username from the database and displays his user details in the profile_view
page. So profile_view
can be reached from both registration_page
and main_activity
.
The list of methods in profile_view
should work for both registration page and main
activity page but it cause of the intent from the main activity, it throws me a null
pointer exception when i reach from registration page.
Upvotes: 0
Views: 76
Reputation: 24853
There is an attribute named hasExtra()
is available for Intent
. You can make use of that to find out whether an extra is available for it or not.
Try this..
Intent intent = this.getIntent();
if(intent!=null)
{
if(intent.hasExtra("user"))
{
//It means that intent has an extra with tag name - "user".
//So, it shows that the intent is from MainActivity
//Do the necessary coding here
String user = intent.getExtras().getString("user");
Log.i("ProfileView",user);
DBAdapter dbAdapter = new DBAdapter(Profile_view.this);
dbAdapter.open();
String name = dbAdapter.getName(user);
full_name.setText(name);
username_display.setText(dbAdapter.getUserName(user));
mail_display.setText(dbAdapter.getMail_id(user));
date_of_birth_display.setText(dbAdapter.get_date_of_birth(user));
//country_display.setText(dbAdapter.get_country(user));
dbAdapter.close();
}
else
{
//It means that intent doesn't have an extra with tag name - "user".
//So, it shows that the intent is from RegisterActivity
//Do the necessary coding here
}
}
description of hasExtra("")
Upvotes: 1
Reputation: 23638
Pass the string in intent from your mainactivity to identify that you are coming from MainActivity as below:
Intent intent = new Intent(MainActivity.this,Profile_view.class);
intent.putExtra("user",username);
intent.putExtra("Activity","MainActivity");
startActivity(intent);
In your profileview activity check for the value as below:
Intent intent = this.getIntent();
String value= intent.getString("Activity");
if(intent!=null)
{
if(value.equalsIgnoreCase("MainActivity"))
{
String user = intent.getExtras().getString("user");
Log.i("ProfileView",user);
DBAdapter dbAdapter = new DBAdapter(Profile_view.this);
dbAdapter.open();
String name = dbAdapter.getName(user);
full_name.setText(name);
username_display.setText(dbAdapter.getUserName(user));
mail_display.setText(dbAdapter.getMail_id(user));
date_of_birth_display.setText(dbAdapter.get_date_of_birth(user));
//country_display.setText(dbAdapter.get_country(user));
dbAdapter.close();
}
else{
//Comes from Login activity
}
}
Upvotes: 2
Reputation: 12733
Just put one condition for checking value of intent like below:
Intent intent = this.getIntent();
if(intent!=null)
{
if(intent.hasExtra("user"))
{
//Your Code
}
}
Upvotes: 2