简体   繁体   中英

Can't set text to the TextView

I am learning to develop apps for android from a book and after following the instructions I end up with the following. When I run the app the text from the setStartUpScreenText() object is not displayed.

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" /> 

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

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

Fist of all call the setStartUpScreenText() function from inside of setStartUpWorldValues() function (either in the beginning or in the end). You can also call this function inside the onCreate() methods after the setStartUpWorldValues() function.

Secondly replace the entire code of the setStartUpScreenText() function with the following code: (page 90, Learn Android App Development, publisher: 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));

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM