Reputation: 247
I have the following code:
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accounted);
TextView tv1 = (TextView) findViewById(R.id.textView1);
EditText et1 = (EditText) findViewById(R.id.editText1);
String text1 = et1.getText().toString();
When I comment out the "string text1..." line the program works. But if I uncomment it, the application force closes.
TextView(tv1) is on the current page but EditText(et1) is on another page. Can that be the cause of my problem? How do I work around it?
Upvotes: 0
Views: 5725
Reputation: 123
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accounted);
TextView tv1 = (TextView) findViewById(R.id.textView1);
EditText et1 = (EditText) findViewById(R.id.editText1);
String text1 = et1.getText().toString();
This is your code .You have Edittext in another activity thats why the app crashes.If you want to get the text of edittext from last activity to set on textview you will have to add the following line to the code where Intent is used to change the activity.
Intent.putextra("string1",et1.getText().toString());
StartActivity(Intent);
This will definately work for you.
Upvotes: 0
Reputation: 11
this is your code...................
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accounted);
TextView tv1 = (TextView) findViewById(R.id.textView1);
EditText et1 = (EditText) findViewById(R.id.editText1);
String text1 = et1.getText().toString();
as far i m getting you have the edittext in another activity. so for getting text in your textview you have to do some modifications i am showing it hope this will work
first add a edittext in your current activity in which the textview is
editText t1;
after this assign this editText the edittext of another activity
t1=secondactivity.edittext id;
now simply get that text and set on your textview
String text1 = et1.getText().toString();
text1.setText(et1.getText().toString());
Upvotes: 0
Reputation: 1480
You can't access the textview from another activty you should pass the value of the textview to the second activity using intents
example available at How do I pass data between Activities in Android application?
Upvotes: 2
Reputation: 11571
If EditText is in different xml then dont use it. Add EditText either in your xml or remove it from source code.
Upvotes: 0