What is false in my program that gives “App keeps stopping” while opening that activity?

前端 未结 2 1893
别跟我提以往
别跟我提以往 2021-01-29 14:57

Application open normal but while opening this activity it says \"App keeps stopping\". I don\'t understand what is the problem. Maybe API level is not compatible with calendar.

相关标签:
2条回答
  • 2021-01-29 15:40

    There is no layout attached to your activity before setContentView function call so initialize your views after the layout is attached to your activity

    Should be

    // global reference
    EditText etDate;
    
    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_ride);
        // initialization after the layout is attached to activity
        etDate=(EditText) findViewById(R.id.etDate);
    
    0 讨论(0)
  • 2021-01-29 15:46

    You cannot set this code outside of OnCreate method. Because your layout has not been created yet, you cannot findViewById since they activity has no content. You must setContentView before you do anything regarding your views that will be added to your activity.

    final EditText etDate=(EditText) findViewById(R.id.etDate);
    

    Set these objects without initializing them, and then initialize in your onCreate after you setContentView(LAYOUT);

    EditText etDate;
    
    public void onCreate(Bundle savedIntanceState) {
        ....
    
        etDate = findViewById(R.id.etDate);
    
    0 讨论(0)
提交回复
热议问题