package com.example.myapplication_1; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { public Button sum; public Button minus; public Button umn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } sum = (Button) findViewById(R.id.sum); minus = (Button) findViewById(R.id.minus); umn = (Button) findViewById(R.id.umn); @Override public void onClick(View v) { if (v == sum) { Intent intent = new Intent(MainActivity.this, sum.class); startActivity(intent); }else if (v == minus){ Intent intent = new Intent(this, minus.class); startActivity(intent); } } } 

Why is the second Override highlighted in red, and why is it not possible to switch to another page without it? Java has just started

  • 2
    The code should be a code and not a link. And the answer is simple, you did not connect the interface in the class declaration. impliment OnClickListener or something. - pavel
  • You can detail, I just started to do - Alex
  • more - from the wrong side you started. That's when you connect the buttons, you connect the handler - then you and the IDE will tell you what to write. Now she says that you have written an unnecessary method. PS very rough - pavel
  • What is the right way then? - Alexey
  • but it depends on what you need) the buttons are all null not created anywhere. The handler is not stuck. Resource files are not attached. - pavel

1 answer 1

Red underlines you, because the onClick method onClick stated in the OnClickListener interface, which you in turn forgot to implement in your activity . Roughly speaking, the code for your activity should look like this:

 public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Button summButton = (Button) findViewById(R.id.sum); Button minusButton = (Button) findViewById(R.id.minus); Button umnButton = (Button) findViewById(R.id.umn); summButton.setOnClickListener(this); minusButton.setOnClickListener(this); umnButton.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.sum: startActivity(new Intent(MainActivity.this, sum.class)); break; case R.id.sum: startActivity(new Intent(MainActivity.this, minus.class)); break; } } } 

More information can be found here . And I also advise you to read about java code conventions.