How to reference a button in an xml from another Activty?

0

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?     

asked by anonymous 04.01.2017 / 02:48

1 answer

1

In an activity you can only instantiate the buttons that are in the XML that you set in setContentView(R.layout.activity_main);
If you try to instantiate a button from another XML, it is null pointer.

    
04.01.2017 / 14:50