更改android中单独活动中的textview

And*_*ndy 2 android textview android-activity

我有一个从数据库中提取信息的登录页面,然后我想使用这些信息在新页面/活动上填充不同的文本视图.我可以使用textview来更改我的提交按钮的活动,但是当我尝试更改第二个活动的textview时,它只是崩溃了(应用程序意外停止).

这是我更改textview的代码(其中txtID是我在单独活动上的textview)

TextView test2 = (TextView) findViewById(R.id.txtID);
test2.setText(test);
Run Code Online (Sandbox Code Playgroud)

我的xml用于单独活动

<TextView android:text="TextView" android:id="@+id/txtID"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
Run Code Online (Sandbox Code Playgroud)

哦,我正在为我的登录页面使用tableview,然后使用我的其余页面的标签.我对这个很新,很抱歉,如果这很简单,但任何帮助都会非常感激!! :-)

Cor*_*old 7

您不希望直接触摸另一个Activity的UI元素.您可以使用捆绑来回传递信息.这是一个例子:

假设我们有活动A,并且它有一些信息作为它想要传递的String,成为活动B中TextView的文本.

//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);
Run Code Online (Sandbox Code Playgroud)

所以现在在Activity B的onCreate方法中,我们可以获得该String并将其作为文本分配给TextView,就像你有

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);     
}
Run Code Online (Sandbox Code Playgroud)