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.
Related
I read some example on this website and other but still have error.
It's not a compilation error but my application crash when I click on the button.
There is code of my MainActivity.java (only interesting part) :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//blablabla
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.myLayout= (LinearLayout) findViewById(R.id.layoutProp);
Button button = (Button) findViewById(R.id.buttonValider);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivity2();
}
});
}
public void openActivity2() {
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(EXTRA_TEXT, text); // this one does not work
intent.putExtra(EXTRA_NUMBER, nbTextView);
startActivity(intent);
}
}
And code in Activity2.java
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
Intent intent = getIntent();
int nbTextView = intent.getIntExtra(MainActivity.EXTRA_NUMBER, 0);
MyTextView[] text = (MyTextView[]) intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
TextView textView1 = (TextView) findViewById(R.id.TVtest);
textView1.setText(""+ nbTextView);
} }
if I comment : MyTextView[] text =(MyTextView[])intent.getSerializableExtra(MainActivity.EXTRA_TEXT);
the application does not crash.
Thank you all very much for your help
You cannot pass views through intents. Views are part of the layout and cannot be shared between activities.
If you want to pass array with custom objects you must implement a Parcelable or Serializable class and use:
getIntent().getParcelableExtra("array");
or
getIntent().getSerializableExtra("array");
Parcelable class will be faster but it is harder to implement.
Another way is to use a library like Gson to parse your data to Json and pass it as a string.
I just figured out how to use intent to pass variables to other activity ,but I found out that it has a limit , it can only pass one variable to one activity .
I want to know how to share the variable in multiple activity like I have this variable in activity 1 and I want to use it in activity 2 and activity 3.
this is my activity 1 , the variable I want to use in other activity is this
(double ans=(90+4.8*nh+13.4*nw-5.7*na);)
I figured out how to pass it to activity 2 but I can't pass it to activity 3. Is there an easy way to pass the variable in multiple activity , because I'm kinda new to Android and OOP there's a lot of stuff I don't really get just from searching. Thank you so much!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_male);
age =(TextView)findViewById(R.id.age);
height=(TextView)findViewById(R.id.heigth);
weight =(TextView)findViewById(R.id.weigth);
result =(TextView)findViewById(R.id.result);
eage=(EditText)findViewById(R.id.eage);
eheight=(EditText)findViewById(R.id.eheight);
eweight=(EditText)findViewById(R.id.eweight);
calculate=(Button)findViewById(R.id.calculate);
back =(Button)findViewById(R.id.back);
next =(Button)findViewById(R.id.next);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double na=Double.parseDouble(eage.getText().toString());
double nh=Double.parseDouble(eheight.getText().toString());
double nw=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*nh+13.4*nw-5.7*na);
result.setText("Your BMR is "+ans);
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i=new Intent(MALE.this,MainActivity.class);
startActivity(i);
}
});
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MALE.this, MALERUN.class);
double na=Double.parseDouble(eage.getText().toString());
double nh=Double.parseDouble(eheight.getText().toString());
double nw=Double.parseDouble(eweight.getText().toString());
double ans=(90+4.8*nh+13.4*nw-5.7*na);
result.setText("Your BMR is "+ans);
i.putExtra("answer",ans);
i.putExtra("age",na);
i.putExtra("height",nh);
i.putExtra("weight",nw);
startActivity(i);
}
});
}
Use SingleTon Pattern.
public class SingletonClass {
private double yourValue;
private static SingletonClass sSoleInstance;
private SingletonClass() {
} // private constructor.
public static SingletonClass getInstance() {
if (sSoleInstance == null) {
sSoleInstance = new SingletonClass();
}
return sSoleInstance;
}
public void setValue(double val) {
yourValue = val;
}
public double getValue() {
return yourValue;
}
}
Set Variable from Activity1
SingletonClass.getInstance().setValue(yourValue);
Now you can access this value across application.
SingletonClass.getInstance().getValue();
For more details click here
Using SharedPreferences you can get it.here is examples
next.setOnClickListener{
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("answer",ans);
editor.putString("age",""+na);
editor.putString("height",""+nh);
editor.putString("weight",""+nw);
editor.commit();
Intent i=new Intent(MALE.this,MainActivity.class);
startActivity(i);
}
Any Another Activity you can get it below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(this);
String answer = sharedPref.getString("answer",null);
String age = sharedPref.getString("age",null);
Log.e("resultsss",""+answer + " age : "+age);
}
Solution 2:
Either you have to use Sqlite database
suggestion: if you have very less fields better to use
SharedPreferences if you have little hug data better go with Sqlite
database
You can declare a varible :
public class MainActivity extends AppCompatActivity {
// Like below
public static double ans;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
}
And call it from everywhere like this:
MainActivity.ans;
You can declare a different variable anytime you want and it will change in every Activity.
just do this :
MainActivity.ans = 123.123;
I am using Android Studio. I have two activities, MainActivity and Main2Activity. There is an edit text and a button in each one. How do I keep the input in the edit text in any activity when I go to the second activity? I tried many answers but nothing worked.
Here is the code of the activity.
public class MainActivity extends AppCompatActivity {
EditText editText2;
Button button;
String var1 ;
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("var1", var1);
editText2 = (EditText)findViewById(R.id.editText2);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
var1 = savedInstanceState.getString("var1");
var1 = editText2.getText().toString();
editText2.setText(var1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Click1(View view)
{
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
}
To carry over values from one activity to another, you need to add a line of code before startActivity(i);. This line of code that you need to add is:
i.putExtra("inputValue", String.valueOf(editText2.getText()));
Then in the other activity, in the onCreate() method, add this:
Intent intent = getIntent();
String inputValue = intent.getStringExtra("inputValue");
editText.setText(inputValue);
Obviously I don't know the name of your EditText in the MainActivity2 so replace the editText with its name.
Hope this helps!
You need to check for the existence of savedInstanceState in your onCreate function.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for a savedInstanceState
if(savedInstanceState != null){
var1 = savedInstanceState.getString("var1");
editText2.setText(var1); // Making sure that you have assigned editText2 already, of course.
}
}
What is the problem with my code :(
This is the sending class:
public class Send extends AppCompatActivity {
String message_text;
final static String MSG_KEY = "this.is.the.message";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_layout);
}
public void sendMessage(View view) {
EditText entryText = (EditText)findViewById(R.id.message_text);
message_text = entryText.getText().toString();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(message_text, MSG_KEY);
startActivity(intent);
}
}
this is the receiver:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView theMessage = (TextView)findViewById(R.id.theMessage);
theMessage.setText(getIntent().getStringExtra("this.is.the.message"));
}
}
the app starts but it does not pass text, just an empty recieving activity ???
intent.putExtra(message_text, MSG_KEY);
replace with
intent.putExtra(MSG_KEY, message_text);
Frist argument is NAME , second argument - VALUE
You are using value as key and key as value which is causing this issue.
Change
intent.putExtra(message_text, MSG_KEY);
to
intent.putExtra(MSG_KEY, message_text);
If changing intent.putExtra(MSG_KEY, message_text); doesn't fix the issue. Check AndroidManifest.xml to make sure that MainActivity is not the launcher otherwise your MainActivity starts before the send class.
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