我正在从一本书中学习为android开发应用程序,在遵循了说明之后,我最终得到了以下内容。
private void setStartUpScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);我在findViewById()中的dataView1下面看到了红色的波浪形下划线。Eclipse通知dataView1无法解析或不是字段。
完整的代码如下所示,我在所有的dataView下面都有一个红色的波浪形下划线。书中没有提到这个错误。
private void 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));这里是MainActivity
public class MainActivity extends ActionBarActivity {
WorldGen earth = new WorldGen("Earth", 5973, 9.78);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
setStartUpWorldValue();
setStartUpScreenText();
}
protected void setStartUpWorldValue(){
earth.setPlanetColonies(1);
earth.setPlanetMilitary(1);
earth.setColonyImmigration(1000);
earth.setBaseProtection(100);
earth.turnForceFieldOn();
}
private void 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));
}发布于 2014-04-21 07:52:43
您似乎是在fragment_main.xml而不是activity_main.xml.中构建视图的
当您第一次创建新的android项目时,会自动创建和打开以下文件:

然后,在开始时,在fragment_main.xml文件中添加视图(例如: TextView)。最后,您尝试在Activity中使用此视图执行一个基本事件,如下所示:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Using layout activity_main.xml
// You try to set a simple text on the view (TextView) previously added
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("Simple Text"); // And you get an error here!
/*
* You do an fragment transaction to add PlaceholderFragment Fragment
* on screen - this below snippnet is automatically created.
*/
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
} 您不能运行您的应用程序,或者有时您只有一个白屏,因为您试图调用/显示的视图布局错误。
解决方案:将onCreateView方法中的所有内容移到片段类中。
调用视图并在相关片段中执行某些操作,而不是在父活动中。
或者,您可以在活动中执行所有操作,方法是删除不在UI上添加片段的这些行:
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
} 这将在您的活动布局“上方”创建并添加一个片段。然后,在这里设置正确的布局:
setContentView(R.layout.fragment_main); https://stackoverflow.com/questions/23188456
复制相似问题