-1
package com.thenewboston.jordy;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Startingpoint extends Activity {

        @Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startingpoint);
}

int counter;
Button add, sub;
TextView display;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.startingpoint, menu);
    return true;


    counter = 0;
    add = (Button) findViewById(R.id.BAdd);
    sub = (Button) findViewById(R.id.Bsub);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Your total is " + counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText("Your total is " + counter);

        }
    });
}

}

hello, i just started following tutorial to make android apps with eclips. Now I get this error unreachable code at the line with "counter = 0" I think it's has to do something with the line "return true" because, when i remove that the error is gone but then i get other errors :s, does someone know what the problem is here?

Thanks in advance

MAV
  • 7,260
  • 4
  • 30
  • 47
JorDik
  • 127
  • 2
  • 2
  • 7
  • The compiler detects that the code after your `return true;` can never run, and complains about that. It could've issued a warning or something, but as it is, code that's detected to be unreachable at compiletime will give an error. See for example here: http://stackoverflow.com/a/6025364/1015327 – JimmyB Apr 06 '13 at 19:50
  • I think it is a serious conceptual problem too, why are you returning a constant? I suspect this is a case of stringing together multiple code snippet examples... – le3th4x0rbot Apr 06 '13 at 19:53

1 Answers1

3

Any code after the return will never be executed. Therefore you get unreachable code.

Put return at the end of the method

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.startingpoint, menu);

   counter = 0;
   ...
   return true;
}
stefan bachert
  • 9,413
  • 4
  • 33
  • 40