Why Android app crashes for initializing variable with findViewById(R.id.******) at the beginning of the class?

白昼怎懂夜的黑 提交于 2019-11-25 23:25:47

问题


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.

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?


回答1:


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

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.

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




回答2:


Hey I see that you are defining and initialising the instance variables. What I do is I define the the instance variables - EditText username and then in the onCreate method I initialise them - username = (EditText) findViewById(R.id.editText_Username);

The reason you don't initialize the instance variables is because the elements are not ready until after the setContentView in onCreate method - I could be wrong with this, but my best practice is define the instance variable and then initialize them in the onCreate method




回答3:


Before setting setContentView() you are not able to initialize the view items.Because view will not be exists for activity at that time.

Keep the initializations in one separate method and call that method after setting the view to the activity.




回答4:


Add this code

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

in

onCreate(); 

method after

setContentView(R.layout.activity_main);


来源:https://stackoverflow.com/questions/36446114/why-android-app-crashes-for-initializing-variable-with-findviewbyidr-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!