我想更改膨胀视图组的子项
我不知道如何访问小部件

public class MainActivity extends AppCompatActivity {
ScrollView activitymain;
LinearLayout rootLayout, subInfo;
TextView tvSerName, tvSerPrice, tvStarDate, tvNextDate;
CreateSubActivity createSubActivity;


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

    rootLayout = (LinearLayout) findViewById(R.id.root);
    subInfo = (LinearLayout) findViewById(R.id.subinfo);
    activitymain = (ScrollView) findViewById(R.id.activity_main);
    tvSerName = (TextView) findViewById(R.id.tvSerName);   //these are child of subinfo
    tvSerPrice = (TextView) findViewById(R.id.tvSerPrice);
    tvStarDate = (TextView) findViewById(R.id.tvStarDate);
    tvNextDate = (TextView) findViewById(R.id.tvNextDate);

}


在另一个活动中按下按钮时,此方法有效。
添加视图时,我想通过setText()更改tvSerName,tvSerPrice,tvStarDate,tvNextDate的文本,但是它不起作用。
我该怎么办

void addView() {
    createSubActivity = new CreateSubActivity();

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup addview = (ViewGroup) inflater.inflate(R.layout.subinfo, null);
    tvSerName.setText("name"); // it does not works

    rootLayout.addView(addview);
}

最佳答案

您尚未尝试在其上设置文本的视图尚未从viewGroup中设置。

在其上设置文本之前,仍然需要设置tvSerName = addview.findVieById(R.id.tvSerName);

void addView() {
    createSubActivity = new CreateSubActivity();

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup addview = (ViewGroup) inflater.inflate(R.layout.subinfo, null);

    //here
    tvSerName = addview.findVieById(R.id.tvSerName);

    //then set its text
    tvSerName.setText("name"); // it does not works

    rootLayout.addView(addview);
}

07-27 15:59