按钮不工作,如何解决

时间:2013-03-13 15:22:32

标签: android

我有一个带有6个按钮的表单,我目前正试图让第3个工作。前两个按钮工作正常但是当我点击第三个按钮时,什么也没发生,我没有错误。有人可以帮忙吗?

我已经检查了表单名称,类名和按钮名称,但我无法使其工作。我已经包含了按钮的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setting fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
    setContentView(R.layout.main_menu);

    initialize(); //calling this method
}

public void initialize(){

    //Initializing variables

    Film = (Button) findViewById(R.id.bFilm);
    Music = (Button) findViewById(R.id.bMusic);
    Poets = (Button) findViewById(R.id.bPoet);
    Inventors = (Button) findViewById(R.id.bInventors);
    Science = (Button) findViewById(R.id.bScience);
    Sports = (Button) findViewById(R.id.bSport);

    //initialzing onclicklisteners
    Film.setOnClickListener(this);
    Music.setOnClickListener(this);
    Sports.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    //case statment for onclicklisteners
    try{
    switch(view.getId()){
    case R.id.bFilm:
        Intent film = new Intent(this, Film.class); //open film class
        startActivity(film);
        break;
    case R.id.bMusic:
        Intent music = new Intent(this, Music.class); //open music class
        startActivity(music);
        break;
    case R.id.bPoet:
        Intent poet = new Intent(this, Poets.class); //open poet class
        startActivity(poet);
        break;
    case R.id.bInventors:
        Intent invent = new Intent(this, Inventors.class); //open inventors class
        startActivity(invent);
        break;
    case R.id.bScience:
        Intent science = new Intent(this, Science.class); // open science class
        startActivity(science);
        break;
    case R.id.bSport:
        Intent sport = new Intent(this, Sports.class);  //open sports class
        startActivity(sport);
        break;
}

2 个答案:

答案 0 :(得分:2)

如果运动是你的第3个按钮
确保您的xml中没有任何元素bSport期望bSport按钮本身的元素。

删除try{} catch(){}。并查看您的应用是否崩溃,并确保已将Sports活动添加到Manifest

答案 1 :(得分:2)

您只为3个按钮(电影,音乐和运动)设置了点击侦听器,您必须在初始化中设置所有侦听器。

enter code here

public void initialize(){

    //Initializing variables

    Film = (Button) findViewById(R.id.bFilm);
    Music = (Button) findViewById(R.id.bMusic);
    Poets = (Button) findViewById(R.id.bPoet);
    Inventors = (Button) findViewById(R.id.bInventors);
    Science = (Button) findViewById(R.id.bScience);
    Sports = (Button) findViewById(R.id.bSport);

    //initialzing onclicklisteners
    Film.setOnClickListener(this);
    Music.setOnClickListener(this);
    Sports.setOnClickListener(this);
    Poets.setOnClickListener(this);
    Inventors.setOnClickListener(this);
    Science.setOnClickListener(this);
}