1

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" /> 
mmBs
  • 8,421
  • 6
  • 38
  • 46
Ayman Elmubark
  • 959
  • 1
  • 6
  • 11

4 Answers4

0

Make sure you call setContentView method before updating any view in the screen.

Awais Tariq
  • 7,724
  • 5
  • 31
  • 54
0

Make sure you call setStartupScreenText() method after setContentView in oncreate of the activity

Jeffy Lazar
  • 1,903
  • 13
  • 20
  • Stupidly I forgot to add that. However, after I did and tried to run it, I got an error (Unfortunately, Hellow_World has stopped working. The code worked before but some desired text was missing. – Ayman Elmubark Oct 14 '13 at 07:45
0

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));
seaotternerd
  • 6,298
  • 2
  • 47
  • 58
user3124780
  • 83
  • 1
  • 9
-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

}
user3197390
  • 13
  • 1
  • 6