0

I having trouble with making a Textview change text when i push a button from another activity, the button is working from within the same activity as its own but it shuts down my app as soon as I tell it to change text from another activity, my code looks like this:

this is the button code from my main activity:

go = (Button)findViewById(R.id.go);
  go.setOnClickListener(new View.OnClickListener() 
  {

        public void onClick(View v) 
        {
            InventoryActivity.fire.setText("1 fire");

        }
    });

and this is the code from the activity with the TextView I wish to change:

public class InventoryActivity extends Activity

{

public static TextView fire;


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inventory);


    fire = (TextView) findViewById(R.id.fire);
    }
}

ok thanks again

Roy Sorvari
  • 50
  • 1
  • 1
  • 6
  • 3
    "but it shuts down my app" what does that mean? Error message? Log output? Give us something... – SimonSays Apr 30 '13 at 17:15
  • When you say that it `"shuts down my app as soon as I tell it to change text from another activity"`, do you get an exception or some error logged in `logcat`? – HeatfanJohn Apr 30 '13 at 17:15
  • 1
    @SimonSays I think no need of error message here, as the problem is clear.. – Pragnani Apr 30 '13 at 17:22
  • @Pragnani: yea it is pretty clear. Nevertheless, a log output should always be posted too if there is an exception in it. – SimonSays Apr 30 '13 at 17:59

2 Answers2

3

Unfortunately, you wont be able to access view objects from one activity in another activity, when the current inflated layout is from second activity. More info here

My recommended approach: Assuming InventoryActivity will start your SecondActivity, use startActivityForResult() and onActivityResult() in InventoryActivity. In your Second Activity use setResult() with a extra string parameter for the updated text. Example here

You could use shared preferences in other scenarios.

Community
  • 1
  • 1
Sudhee
  • 704
  • 6
  • 12
  • Implement startActivityForResult() and onActivityResult() combo in your Activites. IMO this is the best approach. You can't reference Activities' views from outside the owner Activity. You could using static attributes but it is discouraged because of memory leaks. Hope it helps. – Andres Apr 30 '13 at 17:40
1

fire Textview in InventoryActivity will be null, until it will get its reference in onCreate after setContentView

setContentView(R.layout.activity_inventory); this is where your view will be inflated and drawn to the screen. so it is not possible to Access TextView fire util the layout inflation takes place.

Solution What you can do this second piece of string you want to show in the second screen in intent Extra and the get that extra in second activity and set it to the fire textview.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
  • 1
    I don't know if I would say that `Activity` in Android represents "one screen", seems vague and possibly misleading (to me at least) since `Activity's` can be embedded and be floating windows. – jnthnjns Apr 30 '13 at 17:37
  • @Asok OP is in learning stage, he don't understand complex terms and structure, I have framed that for the simplicity for his understanding...I have removed that as it – Pragnani Apr 30 '13 at 17:38
  • understandable, though one could argue that your setting a misleading foundation for the OP since he is clearly new(ish) to Android. No harm meant by my comment, I still +1'd :) – jnthnjns Apr 30 '13 at 17:40
  • sorry for the confusion, yes my main activity is running R.layout.main my other activity is running on R.layout.activity_inventory – Roy Sorvari May 01 '13 at 15:57
  • so to different layouts are being used – Roy Sorvari May 01 '13 at 15:58