Java 无法获取EditText以显示.setText

Java 无法获取EditText以显示.setText,java,android,Java,Android,我一辈子都搞不明白为什么我不能在编辑文本中显示这个。我设置了两个类(java文件),这是第二个类。即使我使用一个简单的数字(而不是公式),我也无法在应用程序中显示它 我不知道这次我做错了什么。有什么帮助吗 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); to

我一辈子都搞不明白为什么我不能在编辑文本中显示这个。我设置了两个类(java文件),这是第二个类。即使我使用一个简单的数字(而不是公式),我也无法在应用程序中显示它

我不知道这次我做错了什么。有什么帮助吗

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

    totalcostEdit = (EditText) findViewById (R.id.totaltripcostseditText);
    totallodgingEdit = (EditText) findViewById (R.id.totallodgingcostseditText);

}

private void updateStandard ()
{

    //Calculate total lodging costs
    double totallodging = 2;

    totallodgingEdit.setText(String.format("%", totallodging));

}
试试这个

    totallodgingEdit.setText(totallodging + "");

似乎您没有调用
updateStandard()
。你为什么不试试这个:

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

    totalcostEdit = (EditText) findViewById (R.id.totaltripcostseditText);
    totallodgingEdit = (EditText) findViewById (R.id.totallodgingcostseditText);

    updateStandard();

}

private void updateStandard()
{

    //Calculate total lodging costs
    double totallodging = 2;

    totallodgingEdit.setText(String.valueOf(totallodging));

}

看起来你在学习方法

创建
updateStandard()
时,您所做的就是创建它。现在要真正调用它,请在
onCreate
方法中执行以下操作:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main2);
      totalcostEdit = (EditText) findViewById (R.id.totaltripcostseditText);
      totallodgingEdit = (EditText) findViewById (R.id.totallodgingcostseditText);
      updateStandard(); // this is the important part
}

之所以执行
onCreate
而不在任何地方调用它,是因为它实际上在Android库中的某个地方被调用。

你在哪里调用
updateStandard()
?我不知道?这是我第一次用安卓系统上第二节课,我完全迷路了,因为我觉得我比我的课快了一点。我应该发布我的全部代码吗?尝试将其添加为
onCreate()
方法的最后一行。您能更具体一点吗?我对编码还不太熟悉,天哪。所以我明白了你的意思,把它换成了OnCreate。。。成功了!这不管用。输出setText的更好方法。这正是我需要的=)