Display User Input From One Activity To Another In A Textview
Solution 1:
You are passing int
, but receive it in String
, so your app crashed.
MainActivity
int message = R.string.UserInput;
Intent sendMessage = new Intent(MainActivity.this, DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
DisplayMessageActivity
BundleExtra= getIntent().getExtras();
StringtextView= Extra.getString("UserInput");
You should also add
intent.putExtra("UserInput", message);
into launchActivity
method.
Solution 1
Change String textView = Extra.getString("UserInput");
to int textView= Extra.getIntExtra("UserInput", 0);
Solution 2
To send the message to your second Activity, you need to use intent inside your MainActivity button click. Modify your code as below.
package com.example.dell_inspiron.sendmessage;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
importstatic android.R.id.message;
publicclassMainActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Stringmessage= getResources().getString(R.string.UserInput);
finalButtonsend= (Button) findViewById(R.id.send);
send.setOnClickListener(newView.OnClickListener()
{
publicvoidonClick(View v) {
IntentsendMessage=newIntent(MainActivity.this,DisplayMessageActivity.class);
sendMessage.putExtra("UserInput", message);
startActivity(sendMessage);
}
});
}
}
Solution 3
This line look wrong to me
finalString message = getResources().getString(R.string.UserInput);
If you want to get the editText
string, this is the way to go
EditTextinput= (EditText)findViewById(R.id.UserInput); // declare your editTextfinalStringmessage= input.getText().toString(); // get your input type
Solution 2:
You mentioned that you are getting the user input from the MainActivity but i don't see anything like that in your code but you can do the below to pass some value from one activity to another...
Change your second activity like this
BundleExtra= getIntent().getExtras();
StringtextView= Extra.getString("UserInput");
to
IntentExtra= getIntent();
StringtextView= Extra.getStringExtra("UserInput");
and change your MainActivity like this
int message = R.string.UserInput;
to
String message= getResources().getString(R.string.UserInput);
and
finalButtonsend= (Button) findViewById(R.id.send);
send.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(View v) {
launchActivity();
}
});
to
Buttonsend= (Button) findViewById(R.id.send);
send.setOnClickListener(newView.OnClickListener(){
publicvoidonClick(View v) {
startActivity(sendMessage);
}
});
Solution 3:
There is issue with your second Activity.You are passing int value in Intents but trying to get String value.
Change it as below:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
publicclassDisplayMessageActivityextendsAppCompatActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
BundleExtra= getIntent().getExtras();
inttextView= Extra.getInt("UserInput");
TextViewUserInput= (TextView) findViewById(R.id.UserInput2);
UserInput.setText(textView);
finalButtonbutton= (Button) findViewById(R.id.button2);
button.setOnClickListener(newView.OnClickListener()
{
publicvoidonClick(View v) {
launchActivity();
}
});
}
privatevoidlaunchActivity() {
Intentintent=newIntent(this, MainActivity.class);
startActivity(intent);
}
}
Post a Comment for "Display User Input From One Activity To Another In A Textview"