0

I want a Button to invoke a function if it is pressed, but this button is not in the same activity as the one the java.file is related to.

There's no problem with the code, but when I emulate the app, something fails. The button called cambaiarmenu is in another activity, not in Comida2, but I need it there, but working here. So that when it is pressed, the pickEntidad method takes place in this activity.

Here there is the java code of the activity without the button:

public class Comida2 extends AppCompatActivity implements Adaptador2.OnRecipeListener {
    private RecyclerView recyclerView1;
    List<Entidad2> listItems;
    Adaptador2 adaptor;
    private Entidad2 entidad1,entidad2,entidad3;
    Button cambiarmenu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_comida);

        cambiarmenu=(Button) findViewById(R.id.boton_cambiarmenu);

        recyclerView1 = findViewById(R.id.lv_1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView1.setLayoutManager(layoutManager);

        listItems = new ArrayList<>();
        entidad1 = new Entidad2(R.drawable.calabacines_3, "Solomillo a la plancha", " 10 min.", 4, 20);
        entidad2 = new Entidad2(R.drawable.patatas_deluxe_especiadas_70523_300_150, "Entrecot", " 15 min.", 2, 50);
        entidad3 = new Entidad2(R.drawable.tomate, "Hamburguesa", " 2 min.", 5, 100);


        listItems.add(entidad1);
        listItems.add(entidad2);
        listItems.add(entidad3);

        adaptor = new Adaptador2(listItems, this);
        recyclerView1.setAdapter(adaptor);
        adaptor.notifyDataSetChanged();
        Clicado();
    }

    @Override
    public void OnRecipe(int priority) {

        if (priority == 20) {
            Intent in = new Intent(this, Solomillo.class);
            startActivity(in);
        }
        if (priority == 50) {
            Intent in = new Intent(this, Entrecot.class);
            startActivity(in);
        }
        if (priority == 100) {
            Intent in = new Intent(this, Hamburguesa.class);
            startActivity(in);
        }
    }

    private void Clicado(){

        cambiarmenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickEntidad();
            }
        });
    }

    private void pickEntidad(){
        final int random = new Random().nextInt(101);

        int priority1 = entidad1.getPriority();
        int priority2 = entidad2.getPriority();
        int priority3 = entidad3.getPriority();


        listItems.clear();
        if(random < priority1){

            listItems.add(entidad1);

        }else if(random < priority2){

            listItems.add(entidad2);

        }else if (random <= priority3){

            listItems.add(entidad3);

        }
        adaptor.notifyDataSetChanged();
    }
}

And the part with the button is the void clicado.

This is the error message I get when the app crashes: enter image description here

Please if anyone knows what is failing, tell me please. Any help will be good, and thanks.

  • I think the problem is with the id of the button you are trying to find - which is returning null. I suggest you check that... – Leonardo Alves Machado Oct 01 '19 at 16:57
  • It seems as though your button is null when Clicado() is called. Check to see if you defined R.id.button correctly and do a null check right after. This is a good debugging technique – J. Lengel Oct 01 '19 at 16:57
  • The id of the button is correct, may the problem be related with the fact that it is in another activity? @J.Lengel – cristiano ronaldo Oct 01 '19 at 17:01
  • I don't think so. You're not starting a new Activity so it should be fine. You're getting some kind of NullPointerException so try to debug based on this – J. Lengel Oct 01 '19 at 17:03
  • Ok, how do I debug?, i'm a begginer in android studio and I do not know how to do it, neither what it is @J.Lengel – cristiano ronaldo Oct 01 '19 at 17:04
  • I honestly don't know how to debug android studio either but I mean classic debugging where you just put Log.d calls everywhere and check if the values are correct – J. Lengel Oct 01 '19 at 17:07

0 Answers0