Java Android,在启动时为EditText设置文本

Java Android,在启动时为EditText设置文本,java,android,user-interface,Java,Android,User Interface,所以我有一个简单的android应用程序,我从一个文本文件中获取文本(该部分正在工作),我想将该文本设置为EditText.setText()。我遇到的问题是,我无法在onCreate()方法中执行此操作,因为EditText字段和UI尚未创建(看起来是这样的) 我的问题是在哪里做这件事比较好?是否有在GUI元素创建后立即调用的函数 public class MainActivity extends ActionBarActivity { private final String

所以我有一个简单的android应用程序,我从一个文本文件中获取文本(该部分正在工作),我想将该文本设置为EditText.setText()。我遇到的问题是,我无法在onCreate()方法中执行此操作,因为EditText字段和UI尚未创建(看起来是这样的)

我的问题是在哪里做这件事比较好?是否有在GUI元素创建后立即调用的函数

 public class MainActivity extends ActionBarActivity
 {
     private final String FILE_NAME = "awayReply";

     private EditText myEditMessage;

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

    if (savedInstanceState == null) 
    {
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }

}

@Override
protected void onStart()
{
    String message = readMessage();

    myEditMessage = (EditText)findViewById(R.id.edit_message);

    if(myEditMessage != null)
    {
        myEditMessage.setText(message, TextView.BufferType.EDITABLE);
    }
}
XML布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingBottom="16dp"
android:paddingTop="16dp" >

<EditText
    android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/lbl_message"
    android:inputType="textImeMultiLine" />

<Button
    android:id="@+id/btn_update"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/edit_message"
    android:onClick="updateMessage"
    android:text="@string/lbl_update" />


除非使用充气工具充气布局,否则无法在文本视图中设置文本

setContentView(R.layout.main_活动)

在活动的
OnCreate

一旦发生这种情况,您可以:

EditText EditText=(EditText)findViewById(R.id.myEditText)

完整示例:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // this needs to happen first
    EditText editText = (EditText)findViewById(R.id.myEditText)
    editText.setText("my text", TextView.BufferType.EDITABLE); // <-- needs that second parameter
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//首先需要这样做
EditText EditText=(EditText)findViewById(R.id.myEditText)

editText.setText(“我的文本”,TextView.BufferType.EDITABLE);//我想我已经解决了我的问题。我需要在定义onCreateView的占位符片段中完成这项工作。当我在这里调用findviewbyid时,它不会返回null。

是否有任何特定的原因不能通过设置文本值从xml执行此操作?在onStart()中执行此操作或onResume()方法谢谢,我也要试一试。原因是文件中的文本发生了变化,无法硬编码。在oncreate after setContentView中设置
myEditText
的值,而不是在OnStartInterest中,如果没有其他错误,则不应为null。除了我在上一篇评论中提到的内容,我看不到任何内容n你的代码有严重错误。因此肯定有其他因素影响它。我实际上已经在这样做了。我添加了EditText.BufferType.EDITABLE参数,但似乎没有解决问题。请使用
TextView.BufferType.EDITABLE
而不是
EditText.BufferType.EDITABLE
。尝试一下我似乎遇到的问题是t findViewById在onCreate和onStart中不断返回null。onCreate中为null是有意义的,因为它尚未设置,但onStart不应该。只要在setContentView之后设置它,它就不应该是空的,您可以发布onCreate和布局xml的相关部分吗?这将有助于解决问题。向中添加一个字段您的活动
EditText myEditText;
然后在onCreate中将EditText分配给局部变量后,再将其分配给fiekd
this.myEditText=EditText;
,这将为您提供在OnStart和OnResume中使用的内容
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // this needs to happen first
    EditText editText = (EditText)findViewById(R.id.myEditText)
    editText.setText("my text", TextView.BufferType.EDITABLE); // <-- needs that second parameter
}