0

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?

forsvarir
  • 10,749
  • 6
  • 46
  • 77
mucisk
  • 247
  • 5
  • 13

4 Answers4

2

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?

Community
  • 1
  • 1
matsjoe
  • 1,480
  • 12
  • 17
  • ok i can send data but it does not tell how i get the passed data in the other activty. can you tell me how? – mucisk May 08 '11 at 15:42
  • call in the second activity Bundle extras = getIntent().getExtras(); from this Bundle you can extract the data – matsjoe May 08 '11 at 19:39
0

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());
0
 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.

Gurpreet singh
  • 123
  • 2
  • 11
0

If EditText is in different xml then dont use it. Add EditText either in your xml or remove it from source code.

Dinesh Sharma
  • 11,533
  • 7
  • 42
  • 60
  • thanks, but i must use it. get it from another page, and i must show it in another page. – mucisk May 08 '11 at 15:31
  • then use Intent to pass value from one page to another. See http://stackoverflow.com/questions/2091465/how-to-pass-data-between-activities-in-android – Dinesh Sharma May 08 '11 at 15:34