简体   繁体   English

unreachable 语句尝试设置将打开新活动的按钮

[英]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?我在第 30 行 (establecimientos = findViewById(R.id.establecimientos) 上收到一条“无法访问的声明”,同时尝试设置该按钮以将我带到一个新活动。您能帮忙指出哪里出了问题,谢谢?

This is what my my java looks like:这就是我的 java 的样子:

 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.在此之后您有很多代码将永远不会在方法onCreateOptionsMenu()中执行,因为您有一个return语句。 Nothing after the return statement will be executed in this method. return语句之后的任何内容都不会在此方法中执行。

It looks to me like this code should be inside the onCreate() method instead of inside the onCreateOptionsMenu() method.在我看来,这段代码应该在onCreate()方法内,而不是在onCreateOptionsMenu()方法内。

Perhaps you've simply moved code around and gotten confused.也许您只是移动了代码并感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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