Android 没有错误,但强制关闭

Android 没有错误,但强制关闭,android,forceclose,Android,Forceclose,我不熟悉安卓系统。我正在编写一个简单的代码,根据EditText with按钮更改文本视图。我的代码没有错误。但是当我从一个设备上执行它时,我得到一个力接近 这是我的密码: public class MainActivity extends Activity { //EditText et = (EditText)findViewById(R.id.editText1); << Error if uncomment //TextView tv = (TextView

我不熟悉安卓系统。我正在编写一个简单的代码,根据EditText with按钮更改文本视图。我的代码没有错误。但是当我从一个设备上执行它时,我得到一个力接近

这是我的密码:

public class MainActivity extends Activity {

    //EditText et = (EditText)findViewById(R.id.editText1); << Error if uncomment
    //TextView tv = (TextView)findViewById(R.id.textView1); << Error if uncomment

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
公共类MainActivity扩展活动{

//EditText et=(EditText)findviewbyd(R.id.editText1);不能使用
findviewbyd()
就地初始化类变量

public class MainActivity extends Activity {

    EditText  et;
    TextView  tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // move these here
        et = (EditText)findViewById(R.id.editText1);
        tv = (TextView)findViewById(R.id.textView1);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

如果您仔细想想,该版面将仅在调用
setContentView
后初始化,因此在该版面被初始化或设置为活动版面之前,您无法在该版面中找到元素。

请记住始终查看
LogCat
内容,并随您的问题一起发布

试试这个:

public class MainActivity extends Activity {

    // You can use findViewById only once the activity has finished creating, so move the initialization to onCreate function
    EditText  et;
    TextView  tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et = (EditText)findViewById(R.id.editText1);
        tv = (TextView)findViewById(R.id.textView1);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

编辑: 您可以打开
LogCat
如下所示: 单击圆圈标记的按钮(在Eclipse的右下角)

如果没有,您可以按如下方式进行设置:


注:我正在使用mac,但其他操作系统也应该如此。请记住一件事。在调用
setContent
后,请始终调用
findViewById
,否则会出现此类错误。

没问题。很高兴提供帮助:)为什么?为什么?为什么?为什么习惯将类字段称为“全局”呢入侵StackOveflow?它从何而来?Globals的生存期等于整个应用程序,所有类都可以访问。此代码中的变量是类级字段。生存期等于声明的类。作为开发人员,我们应该努力做到准确,这是一个非常恼人的错误。允许它哦,活着,每个noob都在阅读答案,并带着这些答案是全局性的想法离开,因此问题蔓延开来。抱歉@Simon,我修正了我的答案:)你没有任何编译器错误并不意味着你的代码中没有错误。这个答案有什么问题,需要否决吗?@Ameer:看;你的答案重复其他两个答案已经说过的内容。有些人不喜欢这样做,这不会给网站增加价值。@Mat:我没有抄袭其他答案,也没有音译所写的内容,我只是写了我想到的内容。但是,是的,我看不到其他答案。