Getting data from an Intent - java

I am doing a project (Android app) for uni and after a bit of help I have a login screen as per the code below:
//Do once the "Login" button is clicked
public void onClick(View view)
{
//get the users name and password
EditText editName = (EditText) findViewById(R.id.txtUserName);
String name = editName.getText().toString();
//EditText editPassword = (EditText) findViewById(R.id.txtUserPassword);
//String password = editPassword.getText().toString();
//create an Intent object and pass it the name and password
Intent intent = new Intent(this, UserLoggedInScreen.class);
intent.putExtra("userName", name);
//intent.putExtra("userPassword", password);
startActivity(intent);
}
I have commented out the the password bit for now just to get the username bit working. The aim is to click the button and put the text input into textUserName by the user into the String name. Then pass that through to activity UserLoggedInScreen.
Then in UserLoggedInScreen collect the data:
public class UserLoggedInScreen extends Activity
{
TextView welcomeUser;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.userloggedinscreen);
//get the Intent Object from LapMasterActivity
Intent intent = getIntent();
//get the data from the Intent Object
String userName = intent.getStringExtra("userName");
//String userPassword = intent.getStringExtra("userPassword");
welcomeUser = (TextView) findViewById(R.id.txtUserName);
welcomeUser.setText(userName);
}
When I try running it and clicking the button in the opening activity I get the usual "Unfortunately UserLoggedInScreen has stopped working".
I think this bit might be the error:
welcomeUser = (TextView) findViewById(R.id.txtUserName);
I tried changing txtUserName to userName but that didn't help either.
Thanks.

First Thing As Bappy said Register Your activity so that it can be detected at runtime.
<activity android:name="UserLoggedInScreen">
</activity>
Second thing
welcomeUser = (TextView) findViewById(R.id.txtUserName);
EditText editName = (EditText) findViewById(R.id.txtUserName);
You are using same id, are you doing it intentionally??
Pleas Check id of TextView of UserLoggedInScreen.

have you maked activity in AndroidManifast for Second Class........
<activity android:name="UserLoggedInScreen"></activity>

Related

Logging user out Java

I am creating a social app with a login/Registration system and I am having trouble logging the user out.
When I click the logout button I want to unset the username and clear the entire session of the user so that they go back to the LoginActivity class. Right now when I go to the profile activity and click logout I go straight back to the Home Activity which is only suppose to be for user who are logged in. I've been trying since yesterday and still nothing. Can someone help me ?
Login activity:
//SharedPreferences preferences;
private ProgressDialog loadingBar;
private Button LoginButton;
private EditText LoginUsername, LoginPassword;
private TextView NeedNewAccountLink;
private static final String PREF_LOGIN = "LOGIN_PREF";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
SharedPreferences sharedPreferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
}
LoginButton = (Button) findViewById(R.id.login_button);
LoginUsername = (EditText) findViewById(R.id.login_username);
LoginPassword = (EditText) findViewById(R.id.login_password);
NeedNewAccountLink = (TextView) findViewById(R.id.need_new_account_link);
loadingBar = new ProgressDialog(this);
editor.putString("username", String.valueOf(LoginUsername));
editor.putString("pw", String.valueOf(LoginPassword));
editor.apply();
if (LoginUsername != null) {
editor.remove("username");
editor.remove(String.valueOf(sharedPreferences));
editor.remove("pw");
editor.clear();
editor.apply();
SendUserToHomeActivity();
}
private void SendUserToHomeActivity() {
Intent mainIntent = new Intent(LoginActivity.this, HomeActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(mainIntent);
finish();
}
public void OnLogin(View view) {
String username = LoginUsername.getText().toString();
String pw = LoginPassword.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type, username, pw);
}
profile activity:
LogoutButton.setOnClickListener(view -> {
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().clear().apply();
sharedPreferences.edit().remove("username").apply();
sharedPreferences.edit().remove("pw").apply();
editor.remove("username");
editor.remove("pw");
editor.clear();
editor.apply();
finish();
Intent intent = new Intent(ProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
});
LogoutButton.setOnClickListener(view -> {
PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit().clear().apply();
sharedPreferences.edit().remove("username").apply();
sharedPreferences.edit().remove("pw").apply();
editor.remove("username");
editor.remove("pw");
editor.clear();
editor.apply();
Intent intent = new Intent(ProfileActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
});
Remove the Intent.FLAG_ACTIVITY_CLEAR_TOP flag will solve your problem. Please refer to this link.
I believe to get text from edit text you need
LoginUsername.getText().toString()
and Checking EditText "LoginUsername" against If condition will never yield null unless
it points to wrong ID
Try proceeding with appropriate changes in below code block
LoginUsername = (EditText) findViewById(R.id.login_username);
LoginPassword = (EditText) findViewById(R.id.login_password);
NeedNewAccountLink = (TextView) findViewById(R.id.need_new_account_link);
loadingBar = new ProgressDialog(this);
editor.putString("username", String.valueOf(LoginUsername));
editor.putString("pw", String.valueOf(LoginPassword));
editor.apply();
if (LoginUsername != null) {
editor.remove("username");
editor.remove(String.valueOf(sharedPreferences));
editor.remove("pw");
editor.clear();
editor.apply();
SendUserToHomeActivity();
}

How to fix code to sent (name,email,phone,body massage) from android to mail

I use this code to send details to email from android but I just receive body mail only and did not recive name,phone and email .
this my code :
public class ActivityContactUs extends Activity {
EditText name ,email,phone,body;
Button send;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_us);
name = (EditText) findViewById(R.id.name);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
body = (EditText) findViewById(R.id.body);
send = (Button) findViewById(R.id.send);
}
public void SendBtn(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL,
new String[]{getResources().getString(R.string.email_address)});
intent.putExtra(Intent.EXTRA_SUBJECT, getResources().getString(R.string.app_name));
String name_email = name.getText().toString();
intent.putExtra(Intent.EXTRA_TEXT, name_email);
String email_email = email.getText().toString();
intent.putExtra(Intent.EXTRA_TEXT, email_email);
String phone_email = phone.getText().toString();
intent.putExtra(Intent.EXTRA_TEXT, phone_email);
String body_email = body.getText().toString() + "\n\n Sent from Ecommerce Android App";
intent.putExtra(Intent.EXTRA_TEXT, body_email);
startActivityForResult(Intent.createChooser(intent, "Send email..."), 100);
}
}
type order of intent is wrong
intent.setType("plain/text");
should be intent.setType("text/plain");
another problem: Intent.EXTRA_TEXT should be used for only one extra to avoid setting different values associated with the same key

Pass multiple Intents to multiple activities

I want to pass info as shown in this picture screen description
I know that my code is working, becuase with one Intent it works perfectley. However, when I try to put two or more Intents it seems to get messed up. I also checked here to find a sutibale solution, but I don't think I can use it the same way. Thanks in advance
--update--
still dosen't work
i did this on my saving side
Intent saveIntent = new Intent(this, MainActivity.class);
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
startActivity(saveIntent);
and this on my reciving side
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String time = preferences.getString("time",null);
if (time != null)
getTime.setText(time);
Main Activity:
private Button createNewEvent;
private Button showMyEvents;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createNewEvent = (Button) findViewById(R.id.bCreateNewEvent);
showMyEvents = (Button) findViewById(R.id.bShowMyEvents);
}
public void buttonOnClick(View v) {
switch (v.getId()) {
case R.id.bCreateNewEvent:
Intent createNewEventIntent = new Intent(this, CreateNewEventActivity.class);
startActivity(createNewEventIntent);
break;
case R.id.bShowMyEvents:
Intent myEventsIntent = new Intent(this,MyEventsActivity.class);
startActivity(myEventsIntent);
break;
}
}
}
screen 1 save button:
case R.id.bSaveNewEvent:
Intent putIntent = new Intent(getApplicationContext(),MyEventsActivity.class);
// String text = displayTime.getText().toString();
putIntent.putExtra("time",displayTime.getText().toString());
Intent saveIntent = new Intent(this, MainActivity.class);
startActivity(saveIntent);
startActivity(putIntent);
screen 2 getExtras
public class MyEventsActivity extends AppCompatActivity {
TextView getTime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_events);
getTime = (TextView) findViewById(R.id.tvGetTime);
Intent in = getIntent();
String name = in.getStringExtra("time");
getTime.setText(name);
/* Bundle extras = getIntent().getExtras();
if (extras != null){
getTime.setText(extras.getString("time"));
}*/
If you are trying to save data that will be need to be used in multiple other activities, SharedPreferences will be the easiest solution.
https://developer.android.com/training/basics/data-storage/shared-preferences.html
When you want to save a value, you would use:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("time", displayTime.getText().toString());
editor.commit();
Then when you want to get the preference later on you would use:
SharedPreferences preferences = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String time = preferences.getString("time", "");

Android app crashing on using editText

I am new to Android and I'm trying to make an app that sends some data from a secondary activity to the initial one and after the data is sent the second activity is closed. Problem is I get this error when compiling
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String java.lang.Object.toString()' on a null object reference
public void sendData(View view)
{
Bundle bundle = new Bundle();
String msg = new String("");
String msg2 = new String("");
Intent intent1;
intent1 = new Intent(this, MainActivity.class);
EditText editText1 = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
msg = editText1.getText().toString();
msg2 = editText2.getText().toString();
bundle.putString(extra_message,msg);
bundle.putString(extra_message2,msg2);
intent1.putExtras(bundle);
startActivity(intent1);
finish();
}
I've seen other posts related to this. I've tried to initialize the strings that capture the value of the editText yet it says its null and causes my app to crash. Below I have the code that conjures my listview and some items and the point is to add another element from the second activity:
ListView list = (ListView) findViewById(R.id.listViewMain);
String[] itemsToBeDisplayed = {"Vezi Doctor", "Cumparaturi", "Facturi", "Datorie"};
arrayList=new ArrayList<>(Arrays.asList(itemsToBeDisplayed));
adapter=new ArrayAdapter<String>(this,R.layout.list_items,R.id.txtitem,arrayList);
list.setAdapter(adapter);
Intent intent = getIntent();
Bundle bundle = this.getIntent().getExtras();
String new_note = bundle.getString(Add_note.extra_message);
String notes = bundle.getString(Add_note.extra_message2);
arrayList.add(new_note);
It is within the onCreate Method. The rest of the code works just fine.
I think: In order to pass data, such as bundles, you need to also set a variable name, not just a value. What about :
intent1.putExtras("blabla",bundles);

How to pass integer to a new activity in Android?

I'm new to android. I am taking a string and a numerical value as input from the MainActivity.
On pressing the button, the following method is called which invokes the second activity.
Am I passing the values correctly?
If yes, how do I receive both these values for use in the second activity and then print them ?
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
intent.putExtra("lol",message);
startActivity(intent);
}
P.S. I know Im not passing the integer at all. I dont know how to do that. Please help!
From the new Activity you should call getIntent(): https://developer.android.com/reference/android/app/Activity.html#getIntent()
Once you have the Intent you have to call getIntExtra(String, int): https://developer.android.com/reference/android/content/Intent.html#getIntExtra(java.lang.String,%20int).
In your case, from the new Activity something like:
Intent intent = getIntent();
int lol = intent.getIntExtra("lol", 0);
Or shorter:
int lol = getIntent().getIntExtra("lol", 0);
That '0' is the default value for the int if the extra "lol" does not exist.
You just use this one for integer values
In second activity....
int i = getIntent().getIntExtra(SendingStringName, 0);
(or)
String progress = getIntent().getStringExtra(SendingStringName);
(or)
ArrayList< String> progress = getIntent().getStringArrayListExtra(name);
To send the data:
EditText editText1 = (EditText) findViewById(R.id.name);
EditText editText2 = (EditText) findViewById(R.id.ma);
String message = "Hi ! " + editText1.getText().toString();
int i = Integer.parseInt(editText2.getText().toString());
Bundle param = new Bundle();
param.putString("greeting",message);
param.putInteger("NumberInteger",i);
Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtras(param);
startActivity(intent);
To receive them:
In Class DisplayMessageActivity.java do:
Intent it = getIntent();
Bundle param = it.getExtras();
String capturedMessage= param.getString("greeting");
Integer captured_I = param.getInteger("NumberInteger");

Categories

Resources