1

我正在学习从一本书中开发适用于 android 的应用程序,在按照说明进行操作后,我得到了以下结果。当我运行应用程序时,setStartUpScreenText()不会显示来自对象的文本。

MainActivity.java:

protected void setStartupScreenText(){
        TextView planetNameValue = (TextView)findViewById(R.id.DataView1);
        planetNameValue.setText(earth.planetName);
}

MainActivity.xml

<TextView
        android:id="@+id/DataView1"
        android:layout_toRightOf="@+id/TextView1"
        android:layout_marginLeft="36dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 
4

4 回答 4

0

确保在活动的 oncreate之后调用setStartupScreenText()方法setContentView

于 2013-10-14T05:41:28.260 回答
0

首先setStartUpScreenText()从函数内部调用setStartUpWorldValues()函数(在开头或结尾)。您也可以在函数onCreate()之后的方法内调用此setStartUpWorldValues()函数。

其次将函数的整个代码替换为setStartUpScreenText()以下代码:(第90页,Learn Android App Development,发布者:Apress)

            TextView planetNameValue = (TextView) findViewById(R.id.dataView1);
    planetNameValue.setText(earth.planetName);
    TextView planetMassValue = (TextView) findViewById(R.id.dataView2);
    planetMassValue.setText(String.valueOf(earth.planetMass));
    TextView planetGravityValue = (TextView) findViewById(R.id.dataView3);
    planetGravityValue.setText(String.valueOf(earth.planetGravity));
    TextView planetColoniesValue = (TextView) findViewById(R.id.dataView4);
    planetColoniesValue.setText(String.valueOf(earth.planetColonies));
    TextView planetPopulationValue = (TextView) findViewById(R.id.dataView5);
    planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
    TextView planetMilitaryValue = (TextView) findViewById(R.id.dataView6);
    planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
    TextView planetBaseValue = (TextView) findViewById(R.id.dataView7);
    planetBaseValue.setText(String.valueOf(earth.planetBases));
    TextView planetForceFieldValue = (TextView) findViewById(R.id.dataView8);
    planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
于 2013-12-21T06:52:13.407 回答
0

确保setContentView在更新屏幕中的任何视图之前调用方法。

于 2013-10-14T05:30:55.763 回答
-1

protected void setStartUpWorldValues() {

    earth.setPlanetColonies(1);  //Set planet colonies to 1
    earth.setPlanetMilitary(1); // set planet military to 1 
    earth.setColonyImmigration(1000); // set planet population to 1000 
    earth.setBaseProtection(100);   // set Planet armed force to 100
    earth.turnForceFieldOn();  // Turn on the planet force field

 setStartUpScreenText() ;
    TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
    planetNameValue.setText(earth.planetName);
    TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
    planetMassValue.setText(String.valueOf(earth.planetMass));
    TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
    planetGravityValue.setText(String.valueOf(earth.planetGravity));
    TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
    planetColoniesValue.setText(String.valueOf(earth.planetColonies));
    TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
    planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
    TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
    planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
    TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
    planetBasesValue.setText(String.valueOf(earth.planetBases));
    TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
    planetForceFieldValue.setText(String.valueOf(earth.planetProtection));      
}
private void setStartUpScreenText() {
    // TODO Auto-generated method stub

}
于 2014-01-15T09:06:02.987 回答