How to reference an xml button of another Activty?

Asked

Viewed 782 times

0

This is the mistake: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.octupus.ramonteste/com.example.octupus.ramonteste.projetoEscolaAluno.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 am trying to access an xml button from another Activity, ai generates the above error, how to resolve ?

1 answer

1


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

  • But how can I solve this Mr_anderson ? pq and I need to take the values from the field of another Activity and also need to use the "SAVE" button in another xml. Help me please ! rsr

  • Let’s assume you have Activity A and B. a Activity A is the register and a B is the confirmation. Then Voce wants for example the user to fill in Activity A and when it is to confirm, call Activity B. There you want Activity B to get the data from activitty A? That’s about it?

  • 1

    That’s right, Mr_andreson. But I got through Intent and Bundle, in the end I called Intent with Bundle with this <startActivityForResult(it, EXTRA_MESSAGE)>, and it worked. I’m grateful for your help.

  • I was going to suggest +- that. So that’s good.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.