eclipse android应用程序语法错误

时间:2014-10-20 11:58:35

标签: java android eclipse

我刚开始使用eclipse,我试图做我的第一个Android应用程序。但显然我的语法错误有问题,我不知道如何解决它或我在哪里做错了。所以我希望有人可以帮助我。谢谢:)原谅我的英语不好。

红色下划线的是错误,但我不知道如何修复它们。

从这里开始

这是我的.java

public class Category extends Activity { <---(**this sign is underlined in red**)

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

        Button switchButton = (Button) findViewById(R.id.button1);

        switchButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Category.this, Cake.class);
                startActivity(intent);

        Button switchButton = (Button) findViewById(R.id.button2);

        switchButton.setOnClickListener(new View.OnClickListener() {

                @Override
            public void onClick(View v) {
                Intent intent = new Intent(Category.this, Cookie.class);
                startActivity(intent);

            };
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }                         ^
              (**this sign is underlined in red**)

};

1 个答案:

答案 0 :(得分:1)

不幸的是,您尝试在另一个按钮侦听器中初始化按钮侦听器。那是错的。

修正:

 Button switchButton = (Button) findViewById(R.id.button1);

 switchButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Category.this, Cake.class);
        startActivity(intent);

   };
  });

  Button switchButton2 = (Button) findViewById(R.id.button2);

  switchButton2.setOnClickListener(new View.OnClickListener() {

        @Override
    public void onClick(View v) {
        Intent intent = new Intent(Category.this, Cookie.class);
        startActivity(intent);

    };
});