Java 表达式 预期错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39878292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:31:42  来源:igfitidea点击:

Expression Expected error

javaandroidbuttonandroid-activity

提问by Max zert

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

在 Android Studio 中,我想要一个按钮来启动一个新的活动。我收到表达式预期错误。我哪里错了?

@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);
    }
  });
}

回答by Filipe Gon?alves

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).

您必须删除此行Button advanceToQuestionTwo = {Button};或完成它。你需要把(Button)而不是{Button}。然后你应该打电话给findViewById( id of that button in the xml file).