dataView出错

时间:2014-04-20 22:49:17

标签: android

我正在学习从一本书开发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));


}

1 个答案:

答案 0 :(得分:0)

您似乎在 fragment_main.xml 而非 activity_main.xml 中构建了视图。

当你第一次创建一个新的android项目时,你会自动创建和打开这些文件:

enter image description here

然后,当您开始时,在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方法中的所有内容移动到Fragment类中。

调用视图并在相关片段中执行某些操作,而不是父活动


或者你可以在活动中完成所有操作,删除这些不在你的UI上添加片段的行:

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}  

以上创建并在您的活动布局“上方”添加一个片段。然后,在此处设置正确的布局:

setContentView(R.layout.fragment_main);