无法将文本设置为TextView

时间:2013-10-14 05:29:41

标签: java android textview displayobject

我正在学习从一本书开发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 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

所有拳头都从setStartUpScreenText()函数内部调用setStartUpWorldValues()函数(在开头或结尾)。您也可以在onCreate()函数后的setStartUpWorldValues()方法中调用此函数。

其次,使用以下代码替换setStartUpScreenText()函数的整个代码:(第90页,学习Android应用程序开发,发布者: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));

答案 3 :(得分:-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

}