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

后端 未结 4 1453
萌比男神i
萌比男神i 2020-11-22 09:27

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

public class MainActivity extends AppCompatA         


        
4条回答
  •  Happy的楠姐
    2020-11-22 09:54

    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.

提交回复
热议问题