I am trying to pull in a string from one activity to another but every time I try to open the one activity I get a forceclose on .getString.
Logcat Error
10-26 10:36:45.444: ERROR/AndroidRuntime(15112): at http.www.hotapp.com.timeandlocation.email.EmailSettings.onCreate(ThisActivity.java:29)
Activity Calling the string
public class ThisActivity extends Activity{
private TextView reciever;
private String rec;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.elayout);
reciever = (TextView) findViewById (R.id.Reciver);
rec =
getIntent()
.getExtras()
.getString
("send");
reciever.setText(rec);
}
Activity with the string
public class OtherActivity extends Activity{
private EditText sendto;
private Button save;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.emailactivity);
sendto = (EditText) findViewById(R.id.sendto);
save = (Button) findViewById(R.id.Save);
save.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent (EmailActivity.this,EmailSettings.class);
intent.putExtra("send", sendto.getText().toString());
startActivity(intent);
}
});
}
}
Thanks for the help.
try using getIntent().getStringExtra("send");
If you want to check if the extra is there use:
getIntent().hasExtra("send");
Try this:
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
rec = extras.getString("send");
}
try this by eliminating NULL EXCEPTION from getString() function
Related
I have a String value which is the text from an EditText and I want to use that String value in another activity.
In a TextView the user puts his/her name and in the next activity I want to have a Welcome screen that says, Hello, name
package com.example.aprendelastablasdemultiplicar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class pantalla2 extends AppCompatActivity {
private EditText ingresarnombre;
private TextView cifracero;
private TextView cifrauno;
ingresarnombre = (EditText)findViewById(R.id.ingresarnombre);
String nombre = ingresarnombre.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
cifracero.setText(0);
cifrauno.setText(1);
}
}
In your first activity where you will put the name on the edittext, just get the string from the Edittext and pass that with intent.
FirstActivity :
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText edittext = (EditText)findViewById(R.id.edittext);
String name = edittext.getText().toString();
Intent intent = new Intent(ActivityOne.this,ActivityTwo.class);
intent.putExtra("name_extra",name);
startActivity(intent);
}
}
On your second activity just receive the string extra value from intent and work with it.
Second Activity :
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String name = getIntent().getStringExtra("name_extra");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Welcome "+name);
}
}
Create a button in your pantalla2 so that on button click you can go to another activity.And make sure you have given the id of your textview and button in activity_pantalla2 See this it will work:
public class pantalla2 extends AppCompatActivity {
private EditText ingresarnombre;
private Button button;
private TextView cifracero;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantalla2);
cifracero = findViewById(R.id.txtCifracero);
button = findViewById(R.id.button);
String cifracero = cifracero.getText().toString().trim();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("key", cifracero);
startActivity(intent);
}
});
}
}
In your SecondActivity you just have to add textview in your layout and get the value send from first activity.
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String name = getIntent().getStringExtra("name_extra");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Welcome "+name);
}
}
I am trying to send data through an activity to another but there is a variable error that I didn't understand, please Help. Thank you very much for your time and assistance in this matter.
public class MainActivity extends AppCompatActivity {
EditText Message_Text;
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View views) {
Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
}
the second activity code is:
public class SecondActivity extends AppCompatActivity {
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(35);
// setContentView(R.layout.second_layout);
}
}
you have to write like below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendMessage();
}
public void sendMessage() {
EditText Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra("MESSAGE_KEY",message);
startActivity(intent);
}
I'm a PHP developer but today I need to face the Android Studio.
I want to send an input text to another activity. But I had some errors:
error: non-static method putExtra(String,String) cannot be
referencedfrom a static context error: cannot find symbol method
StartActivity(Intent)
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.proj.proj";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, main.class);
EditText editText = (EditText) findViewById(R.id.main);
String message = editText.getText().toString();
Intent.putExtra(EXTRA_MESSAGE, message);
StartActivity(intent);
}
}
what is wrong?
Use proper instance of intent not Intent.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, main.class);
String message = editText.getText().toString();
intent.putExtra("com.proj.proj", message);
startActivity(intent);
}
}
Can someone help me? How do I open a new activity after passing basic login argument, here's my code and I don't know what's going, I get an error:
public class MainActivity extends AppCompatActivity {
EditText usern = (EditText)findViewById(R.id.user_name);
String user_name = usern.getText().toString();
EditText passw = (EditText)findViewById(R.id.password);
String pass_word = passw.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
#Sheldon Madison : Try this way . Need proper Initialization of Global or local variables .
Please read Official Documents
http://developer.android.com/intl/es/index.html
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
EditText usern = (EditText)findViewById(R.id.user_name);
String user_name = usern.getText().toString();
EditText passw = (EditText)findViewById(R.id.password);
String pass_word = passw.getText().toString();
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
Try
public class MainActivity extends AppCompatActivity {
EditText usern,passw;
String user_name,pass_word;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usern = (EditText)findViewById(R.id.user_name);
passw = (EditText)findViewById(R.id.password);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
user_name = usern.getText().toString();
pass_word = passw.getText().toString();
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
I don't know why, but this isn't working. Can anyone see anything wrong with my code.
I have two activities and I'm passing data from two
public class InputActivity extends AppCompatActivity {
EditText number1EditText;
EditText number2EditText;
Button addButton;
InputActivity code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
number1EditText = (EditText)findViewById(R.id.editText);
number2EditText = (EditText)findViewById(R.id.editText2);
addButton = (Button)findViewById(R.id.button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(getApplicationContext(), AddActivity.class);
mIntent.putExtra("number1", number1EditText.getText().toString());
mIntent.putExtra("number2", number2EditText.getText().toString());
startActivity(mIntent);
}
});
}
AddActivity code looks like this
public class AddActivity extends AppCompatActivity {
TextView answer;
double y=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
answer =(TextView)findViewById(R.id.textView);
String value1 = getIntent().getExtras().getString("number1");
String value2 = getIntent().getExtras().getString("number2");
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
}
Just looking quickly:
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
You're passing a int to the method setText() which happens to have an overload that receives a int argument for the cases when you pass the reference from a String in some xml. You may be getting a ResourceNotFoundException.
If you want to show a text with the sum between your values:
answer.setText(String.valueOf((int) (Double.parseDouble(value1)+Double.parseDouble(value2))));
Just keep in mind that there are a lot of checks you have to do first, you may get another exceptions doing this kind of parse, like NumberFormatException for instance.