为什么Android应用程序在类开始时因使用findViewById(R.id.******)初始化变量而崩溃? [英] Why Android app crashes for initializing variable with findViewById(R.id.******) at the beginning of the class?

查看:434
本文介绍了为什么Android应用程序在类开始时因使用findViewById(R.id.******)初始化变量而崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是测试输入/输出,没有特殊用途,这是成功运行的最后一个代码:

I was just testing input/output, there was no special purpose, this is the last code that had run successfully:

public class MainActivity extends AppCompatActivity
{

/*
EditText username = (EditText)findViewById(R.id.editText_Username);
EditText password = (EditText)findViewById(R.id.editText_Password);
TextView inputdata = (TextView)findViewById(R.id.textView_InputData);
TextView welcome = (TextView)findViewById(R.id.textView_Welcome);
Button login=(Button)findViewById(R.id.button_Login);
Button anotherLogin=(Button)findViewById(R.id.button_Login_Another);

public void doLoginOnClick(View v)
{
    String s1=username.getText().toString();
    inputdata.setText(s1);
}

*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton)findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
} 

}


我正在尝试使用


I am trying to capture id of component using

findViewById(R.id.***)

如您所见,我将这段代码放在代码的开头.

as you can see, I put this code in comment at the very beginning of the code.

EditText username = (EditText)findViewById(R.id.editText_Username);
EditText password = (EditText)findViewById(R.id.editText_Password);
TextView inputdata = (TextView)findViewById(R.id.textView_InputData);
TextView welcome = (TextView)findViewById(R.id.textView_Welcome);
Button login=(Button)findViewById(R.id.button_Login);
Button anotherLogin=(Button)findViewById(R.id.button_Login_Another);

如果我删除了上面的注释并运行了它(在仿真器和真实设备中),程序立即崩溃,我很惊讶这里出了什么问题?

If I remove the comment above and run it (both in emulator and real device), the program crashes immediately, I am surprised what's wrong here?

即使我尝试使用构造函数初始化同一件事.

even I have tried to initialize the same thing using constructor.

但是,如果我将其放入onCreate()中,不会崩溃?为什么?

But if I put it inside onCreate() there's no crash? Why?

我试图获取用户名和密码的信息,并使用

I was trying to fetch info of username and password and display it to the textview in textView_Inputdata using

EditText username = (EditText)findViewById(R.id.editText_Username);
EditText password = (EditText)findViewById(R.id.editText_Password);
TextView inputdata = (TextView)findViewById(R.id.textView_InputData);
inputdata.setText(username.getText.toString()+" "+password.getText.toString());

有更好或更容易的方法吗?

Is there better or easier way to do that?

推荐答案

实例实例初始化时,实例成员变量也将初始化.对于findViewById()

Instance member variables are initialized when the instance itself is initialized. It's too early for findViewById()

onCreate()之前,您的活动尚不具有findViewById()内部需要的Window.在setContentView()(您应该在onCreate()中调用)之前,也没有找到视图.

Before onCreate() your activity does not yet have a Window that findViewById() needs internally. Before setContentView() (that you should be calling in onCreate()) there are no views to be found either.

因此,在onCreate()setContentView()之后初始化视图引用.

Therefore init your view references in onCreate() and after setContentView().

这篇关于为什么Android应用程序在类开始时因使用findViewById(R.id.******)初始化变量而崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆