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);
Related
I have an activity called GatherActivity where I have an EditText. The user can input what ever he wants. Now I need the input of that EditText in a different class, called MapActivity.
I created an Intent to "put it over" in the other activity. But it doesn't work like I aspect it. the object/editText is allways a null object, so nothing is displayed as a markerSnippet.
Here my Code (GahterActivity) in a method onButtonClick():
public void onButtonClick(View view){
EditText editText_markerSnippet = (EditText) findViewById(R.id.editText_markerSnippet);
Intent intent = new Intent(this, MapActivity.class);
intent.putExtra("markerSnippet", editText_markerSnippet.getText().toString());
}
Code in MapActivity:
Bundle extras = getIntent().getExtras();
if(extras != null){
markerSnippet = extras.getParcelable("markerSnippet");
}else{
markerSnippet = "some extra info about your location"
}
in my marker snippet there is no text. so the else case is not in use here...
You're sending a String, but expecting a Parcelable in your activity.
In your MapActivity, change it to:
markerSnippet = extras.getString("markerSnippet");
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");
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>
I am learning android, so I was practising with a program in which I start a new activity with a button and then what ever text was input by the user in the edittext of the previous activity has to displayed in textview of the new started activity but when I pass the user input to the new activity, it doesn't display anything. What can be the problem here is the code:
Lets say this parent activity:
case R.id.Sfr:
Intent data= new Intent(SendData.this, RecieveData.class);
Bundle check = new Bundle();
check.putString("UmerData", cheese);
medt.setText(cheese);
data.putExtras(check);
startActivityForResult(data, 5); // It is used to return data from child activity
break;
The child activity which should show the user input passed to it is:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.recievedata);
Initialize();
Bundle got = getIntent().getExtras();
rt1.setText(got.getString("UmerData"));
}
Why is this child activity not showing userinput which has been passed to it?
Try this -
case R.id.Sfr:
Intent data= new Intent(SendData.this, RecieveData.class);
Bundle check = new Bundle();
check.putString("UmerData", cheese);
medt.setText(cheese);
data.putExtras(check);
startActivity(data);
break;
The child activity which should show the user input passed to it is:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.recievedata);
InitializeFuck();
Bundle got = getIntent().getExtras();
rt1.setText(got.getString("UmerData"));
}
Have a look at these existing answers -
Passing Data between activities
How to pass the Data between Activities
write in first activity:-
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putStringExtra("keyForData", Value);
startActivity(intent);
write code in second Activity:-
Intent intent = Activity2.this.getIntent();
String data =intent.getStringExtra("KeyForData");
1. Use Intent along with putExtra() for sending data to the Another Activity.
Eg:
Intent i = new Intent(Your_Class.this, Desired_Class.class);
i.putExtra("NameKey","name");
startActivity(i);
2. Now use getIntent() on the receiving Activity to get the Starting Intent and then use
getExtras() and getString() to get the String value associated with the key. We have getInt() etc too..
Eg:
Intent intent = getIntent();
String name = intent.getExtras().getString("NameKey");
I've made an intent and i've put inside 2 extras
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra(LABEL_NAME_DEFAULT, lblNameDefault);
intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault);
In my other activity i retrieve the info from them like this
//Get name from the label
Intent intent = getIntent();
String lblNameDefault = intent.getStringExtra(MainActivity.LABEL_NAME_DEFAULT);
String lbl2NameDefault = intent.getStringExtra(MainActivity.LABEL_2_NAME_DEFAULT);
//Set current name to editText
EditText labelNameDefault = (EditText)findViewById(R.id.set_name);
EditText label2NameDefault = (EditText)findViewById(R.id.set_name2);
labelNameDefault.setText(lblNameDefault, TextView.BufferType.EDITABLE);
label2NameDefault.setText(lbl2NameDefault, TextView.BufferType.EDITABLE);
The problem is that i receive to both labelNameDefault and label2NameDefault the result from LABEL_2_NAME_DEFAULT.
Can i only pass one extra?
How can i pass them both?
By default the value of labelName is "Counter 1" and the value of label2Name is "counter 2"
If i comment out intent.putExtra(LABEL_2_NAME_DEFAULT, lbl2NameDefault); the first label name is ok.
It looks like LABEL_2_NAME_DEFAULT is overwriting LABEL_NAME_DEFAULT
You could pass a 'bundle' of extras rather than individual extras if you like, for example:-
Intent intent = new Intent(this, MyActivity.class);
Bundle extras = new Bundle();
extras.putString("LABEL_NAME_DEFAULT",lblNameDefault);
extras.putString("LABEL_2_NAME_DEFAULT",lbl2NameDefault);
intent.putExtras(extras);
startActivity(intent);
Then in your Activity that your triggering, you can reference these like so:-
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Or (if you prefer):-
Bundle extras = getIntent().getExtras();
String username_string = extras.getString("LABEL_NAME_DEFAULT");
String password_string = extras.getString("LABEL_2_NAME_DEFAULT");
Hope this helps! :-)
You can pass two (or more) extras in an intent. You need to make sure that LABEL_2_NAME_DEFAULT and LABEL_NAME_DEFAULT does not have the same value, though.
activity one
Intent intent = new Intent(MainActivity.this, Options.class);
TextView labelName = (TextView) findViewById(R.id.label1); // value = "Counter1"
TextView label2Name = (TextView) findViewById(R.id.label2); // value = "Counter 2"
String lblNameDefault = labelName.getText().toString();
String lbl2NameDefault = label2Name.getText().toString();
intent.putExtra("LABEL_NAME_DEFAULT", lblNameDefault);
intent.putExtra("LABEL_2_NAME_DEFAULT", lbl2NameDefault);
activity two
username_string = getIntent().getExtras().getString("LABEL_NAME_DEFAULT");
password_string = getIntent().getExtras().getString("LABEL_2_NAME_DEFAULT");