0

我有一个带有 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;
}
4

2 回答 2

2

如果 Sports 是您的第三个按钮
,请确保您的 xml 中没有任何带有 idbSport的元素,期望bSport按钮本身。

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

于 2013-03-13T15:25:57.127 回答
2

您只为 3 个按钮(电影、音乐和体育)设置了点击侦听器,您必须在 initlialize 中设置所有侦听器。

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);
}
于 2013-03-13T15:27:45.783 回答