简体   繁体   中英

unreachable statement trying to set up a button that will open a new activity

Im very much new to this. I have a quick question. I get an "unreachable statement on line 30 (establecimientos = findViewById(R.id.establecimientos) while trying to set up that button to take me to a new activity. Can you help pointing at what is wrong, thank you?

This is what my my java looks like:

 package com.example.ceibasxi; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; public class PaginaDos extends AppCompatActivity { Button establecimientos; Button servicios; Button productos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pagina_dos); Toolbar myToolbar = findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_appbar, menu); return true; establecimientos = findViewById(R.id.establecimientos); establecimientos.setOnClickListener(v -> { servicios = findViewById(R.id.servicios); servicios.setOnClickListener(v1 -> { Intent intent1 = new Intent(PaginaDos.this, PaginaServicios.class); startActivity(intent1); productos = findViewById(R.id.producto); productos.setOnClickListener(v11 -> { Intent intent2 = new Intent(PaginaDos.this, PaginaProductos.class); startActivity(intent2);} );});});}}

In your code you have the following:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_appbar, menu);
    return true;

You have a lot of code after this that will never be executed in the method onCreateOptionsMenu() because you have a return statement. Nothing after the return statement will be executed in this method.

It looks to me like this code should be inside the onCreate() method instead of inside the onCreateOptionsMenu() method.

Perhaps you've simply moved code around and gotten confused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM