Android 在onCreate的开始部分使用setText时,无法更改my textView的文本

Android 在onCreate的开始部分使用setText时,无法更改my textView的文本,android,android-activity,textview,oncreate,settext,Android,Android Activity,Textview,Oncreate,Settext,setText在onCreate方法的开始部分使用时无法更改textView的文本,当我将setText保留在onCreate方法的结尾时,它可以将值呈现给android UI 我想知道为什么在start中使用setText时无法将值呈现给UI 保留timeView.setText(日期格式);在Oncreate方法的开始,然后在UI中,我无法看到时间文本值, 其中,保留timeView.setText(dateformatted);在Oncreate方法的末尾,我能够在UI中看到时间文本值 p

setText在onCreate方法的开始部分使用时无法更改textView的文本,当我将setText保留在onCreate方法的结尾时,它可以将值呈现给android UI

我想知道为什么在start中使用setText时无法将值呈现给UI

保留timeView.setText(日期格式);在Oncreate方法的开始,然后在UI中,我无法看到时间文本值, 其中,保留timeView.setText(dateformatted);在Oncreate方法的末尾,我能够在UI中看到时间文本值

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 TextView timeView =   findViewById(R.id.time);
        DateFormat dateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date();
        String dateformatted = dateFormat.format(date);
        timeView.setText(dateformatted);
        Log.d( "Test00" ,dateformatted  );

// Whole rest of code, this way time text value does NOT show up in UI screen

} 


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

// Whole rest of code, this way time value does show up in UI screen

 TextView timeView =   findViewById(R.id.time);
        DateFormat dateFormat = new SimpleDateFormat("HH:mm");
        Date date = new Date();
        String dateformatted = dateFormat.format(date);
        timeView.setText(dateformatted);
        Log.d( "Test00" ,dateformatted  );
} 

我希望在这两种情况下都会显示时间值,因为日志中没有出现错误和警告。

而且您在[rest of code]中没有使用timeView???@Naresh timeView在[rest of code]中,事实上它在onCreate方法本身中,唯一的区别是我在oncreate方法的开始使用timeView.setText,在其他情况下在oncreate方法的结束使用timeView.setText。我在想,是因为android不能这么早就识别,所以没有把它放在最后给它一些时间来执行将值呈现给textView的活动。没有,在代码的其余部分中一定有其他原因,可能会直接或间接地(通过其他视图)影响timeView。你能发布你的布局和代码的其余部分吗?好吧,我不是在我的代码中的任何其他地方玩timeView,最初它是在xml页面中手动给定的一个常量值,但现在我想动态分配当前时间,所以这样做是可行的(最后保持不变),但我只是好奇,为什么它在一开始就不起作用。