5

In Android Studio I want to a button to start a new Activity. I get an Expression Expected error. Where have I gone wrong?

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

  Button advanceToQuestionTwo = {Button};

  Button loadNewActivity = (Button) findViewById(R.id.imageButton);

  loadNewActivity.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
      Intent intent = new Intent(MainActivity.this, Stundenplan.class);
      startActivity(intent);
    }
  });
}
4444
  • 3,541
  • 10
  • 32
  • 43
Max zert
  • 51
  • 1
  • 1
  • 3
  • 1
    Line 6: wrapping a class in curly brackets does not an expression make. – RamenChef Oct 05 '16 at 15:56
  • Possible duplicate of [How to start new activity on button click](http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – ArchiFloyd Oct 05 '16 at 18:04
  • `advanceToQuestionTwo` is an incorrect variable assignment and not needed. Everything else looks okay. If you'd made a [mcve] you might have discovered that – OneCricketeer Oct 05 '16 at 19:01

1 Answers1

2

You must delete this line Button advanceToQuestionTwo = {Button}; or complete it. You need to put (Button) and not {Button}. Then after that you should call findViewById( id of that button in the xml file).

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245