为什么在课程开始时使用 findViewById(R.id.******) 初始化变量时,我的 Android 应用程序会因 NullPointerException 而崩溃? [英] Why does my Android app crash with a NullPointerException when initializing a variable with findViewById(R.id.******) at the beginning of the class?
问题描述
这段代码,注释了顶部的块,运行成功:
This code, with the block at the top commented, runs 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?
我试图获取用户名和密码的信息并将其显示到 textView_Inputdata
中的 textview 使用
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()
之前,您的 Activity 还没有 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()
.
这篇关于为什么在课程开始时使用 findViewById(R.id.******) 初始化变量时,我的 Android 应用程序会因 NullPointerException 而崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!