简体   繁体   中英

Changing a textview in a separate activity in android

I have a log in page that pulls information from a data base, I then want to use this some of this information to populate different textviews on a new page/activity. I can get a textview to change on the activity where I have my submit button, but when I try to change the textview on my second activity, it just crashed (The application has stopped unexpectedly).

Here's my code for changing the textview (where txtID is my textview on a separate activity)

TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);

my xml for seperate activity

<TextView android:text="TextView" android:id="@+id/txtID"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

Oh, I'm using a tableview for my login page, then tabs for my the rest of my pages. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)

You don't want to directly touch the UI elements of another Activity. You can make use of bundles to pass information back and forth. Here is an example:

Say we have Activity A, and it has some information as a String it wants to pass to become the text of a TextView in Activity B.

//Setup our test data
String test = "Some text";
//Setup the bundle that will be passed
Bundle b = new Bundle();
b.putString("Some Key", test);
//Setup the Intent that will start the next Activity
Intent nextActivity = new Intent(this, ActivityB.class); 
//Assumes this references this instance of Activity A
nextActivity.putExtras(b);

this.startActivity(nextActivity);

So now in the onCreate method for Activity B, we can get that String and assign it as the text to the TextView like you have

public void onCreate(Bundled savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); //Setup some layout, set to your own

    String test = getIntent().getExtras().getString("Some Key");
    TextView test2 = (TextView) findViewById(R.id.txtID);
    test2.setText(test);     
}

I'd probably let each separate Activity take care of its own display, and not try to have Activity 1 directly update the display of Activity 2, which is kind of what it looks like you were doing.

The Notepad Tutorial demonstrates an application with two Activities, where one Activity calls another, passing in data. (Take a look at onListItemClick in Notepadv3.) You could maybe follow this model to pass data from Activity 1 to Activity 2, where Activity 2 then takes care of its proper display, using the data it received.

If you're still having problems (like your application crashing), then please post the complete minimal code necessary to replicate your problem. Note the Notepad Tutorial and the Hello, World Tutorial include steps for debugging, which might help you isolate the exact problem.

Andy... If you try to directly touch a UI widget in another activity, your app will crash. Been there, done that accidentally. Instead, consider passing an immutable stateful object between the activities. This can be done using startActivityForResult for instance. I have some sample code here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM