0

I wrote the code to receive name from EditText, but when I initialize EditText variable my app crashes

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class test_activity extends AppCompatActivity {
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_activity);
    }

    final EditText editText = (EditText) findViewById(R.id.editText);**

    public void set_text(View view){
        name = editText.getText().toString();
   }


}
Zaartha
  • 1,106
  • 9
  • 25
  • You need to give *way* more information. At the very least, what's the error? – Carcigenicate Apr 29 '17 at 15:52
  • EditText editText = (EditText) findViewById(R.id.editText); this code needs to come after setContentView(R.layout.activity_test_activity); – Zaartha Apr 29 '17 at 15:57

1 Answers1

0

You need to initialize your views inside onCreate or after it is called. Move your line which is causing the error inside the onCreate method.