2013-10-14 119 views
1

我正在學習從書籍開發Android應用程序,並按照說明完成以下操作。當我運行應用程序時,來自setStartUpScreenText()對象的文本不會顯示。無法將文本設置爲TextView

MainActivity.java:

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

嗨在這裏發佈你的活動課 –

回答

0

確保您在更新屏幕的任何視圖之前調用setContentView方法。

0

確保在OnCreate中活動的調用setStartupScreenText()方法setContentView

+0

愚蠢的我忘了補充一點。然而,在我做了並試圖運行它之後,我得到了一個錯誤(不幸的是,Hellow_World已停止工作。代碼之前工作過,但缺少一些所需的文本。 –

0

所有的拳從setStartUpWorldValues()函數內(在開頭或結尾)調用setStartUpScreenText()功能。您也可以在setStartUpWorldValues()函數之後的onCreate()方法內調用此函數。

其次替換下面的代碼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)); 
-1

保護無效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 

}