0

我创建了5个类。一个类是主要活动,其余是意图活动...我直接对类实现了OnClickListener接口。运行android项目按钮事件时不起作用。源代码如下。

package com.sooraj.cbsroot;

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

public class HomeMainActivity extends Activity implements OnClickListener {
    Button btnBus, btnAbout, btnContact, btnAdmin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_main);
        btnBus = (Button) findViewById(R.id.btnBus);
        btnAbout = (Button) findViewById(R.id.btnAbout);
        btnContact = (Button) findViewById(R.id.btnContact);
        btnAdmin = (Button) findViewById(R.id.btnAdmin);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnBus) {
            Intent intentBus = new Intent(HomeMainActivity.this,
                    BusDetails.class);
            startActivity(intentBus);
        }

        if (v.getId() == R.id.btnAbout) {
            Intent intentAbout = new Intent(HomeMainActivity.this, About.class);
            startActivity(intentAbout);
        }

        if (v.getId() == R.id.btnContact) {
            Intent intentContact = new Intent(HomeMainActivity.this,
                    Contact.class);
            startActivity(intentContact);
        }

        if (v.getId() == R.id.btnAdmin) {
            Intent intentAdmin = new Intent(HomeMainActivity.this,
                    AdminLogin.class);
            startActivity(intentAdmin);
        }

    }

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

}
4

2 回答 2

5

请使用这个

    btnBus.setOnClickListener(this);
    btnAbout.setOnClickListener(this);
    btnContact.setOnClickListener(this);
    btnAdmin.setOnClickListener(this);

在 OnCreate 方法中。

于 2013-07-02T07:39:09.603 回答
2

你忘了注册OnClickListener

   btnBus.setOnClickListener(this);

您必须为每个Button实例执行此操作

于 2013-07-02T07:38:21.880 回答