簡體   English   中英

無法將文本設置為TextView

[英]Can't set text to the TextView

我正在學習從一本書中為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" /> 

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

確保在活動的oncreate中的setContentView之后調用setStartupScreenText()方法

所有的拳調用setStartUpScreenText()函數從內部setStartUpWorldValues()函數(在開頭或結尾)。 您也可以在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));

受保護的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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM