This is the error: java.lang.RuntimeException: Unable to start activity ComponentInfo {com.example.octupus.ramonteste / com.example.octupus.ramp.subject.projectAlumni.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget. Button.setOnClickListener (android.view.View $ OnClickListener) 'on a null object reference
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ListView lista;
public Button btnExcl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//DRAWER LAYOUT
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//cash da listView
lista = (ListView) findViewById(R.id.listView);
btnExcl = (Button)findViewById(R.id.btnExcluir);
final List<Aluno> studentt = escolaAluno();
final ArrayAdapter<Aluno> studentSchoool = new ArrayAdapter<Aluno>(this, android.R.layout.simple_list_item_1, studentt);
lista.setAdapter(studentSchoool);
//metodo onclick do button
btnExcl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editNome);
String name = text.getText().toString();
studentt.add(createAlunos(name));
studentSchoool.notifyDataSetChanged();
}
});
I'm trying to access a button from an xml of another Activity, then it generates the above error, how to solve it?