onClick()中的语法错误

时间:2014-10-20 15:37:40

标签: android eclipse syntax

好的,现在我得到的是如下所示。似乎无法解决它。请为我的最后一年项目的Android开发人员eclipse应用程序提供一些帮助。在youtube和google上讨论了很多关于它的视频但是没有设法帮助修复我在这里的语法错误。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class Category extends Activity {

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

        };

5 个答案:

答案 0 :(得分:1)

检查你的onClick()方法兄...

    switchButton.setOnClickListener(new View.OnClickListener() {

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

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

答案 1 :(得分:0)

在内部onClick方法

的末尾取分号
public void onClick(View v) {
    Intent intent = new Intent(Category.this, Cookie.class);
    startActivity(intent);
};

答案 2 :(得分:0)

替换

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

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

答案 3 :(得分:0)

您没有正确关闭范围。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.lge.cptool.R;

public class Category extends Activity {

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

        });
    }
}

答案 4 :(得分:-1)

只是语法错误。使用此

public class Category extends Activity {


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


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

    switchButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
              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);
}

}
相关问题