为什么Android中的按钮不起作用?

时间:2015-05-30 04:32:27

标签: android button switch-statement logout

我创建了一个菜单活动,其中包含3个按钮(主要,历史记录,注销) 我不知道为什么所有的按钮都不起作用,并且有退出功能 无论如何我不知道退出功能,它是正确的功能还是错误的功能?
我是Android的新手,任何帮助都将不胜感激。

这是我的代码:

public class MenuActivity extends ActionBarActivity implements View.OnClickListener {

        Button mainBtn,historyBtn,logoutBtn ;

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

            mainBtn = (Button)findViewById(R.id.button1);
            historyBtn = (Button)findViewById(R.id.button2);
            logoutBtn = (Button)findViewById(R.id.button3);

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.button1:
                    Intent main = new Intent (this, MainActivity2.class);
                    startActivity(main);
                    break;
                case R.id.button2:
                    Intent history = new Intent(this, HistoryActivity.class);
                    startActivity(history);
                    break;
                case R.id.button3:
                    Intent logout = new Intent(this, LoginActivity.class);
                    keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(logout);
                    this.finish();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_menu, 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();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

5 个答案:

答案 0 :(得分:3)

你做过这个吗?

mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);

即。在代码或xml文件中为按钮注册onClickListener。

答案 1 :(得分:0)

将列表器设置为所有按钮,如..

 mainBtn.setOnClickListener(this);

答案 2 :(得分:0)

您需要使用点击监听器注册按钮。用以下内容替换您的代码..

public class MenuActivity extends ActionBarActivity implements View.OnClickListener {


    Button mainBtn,historyBtn,logoutBtn ;


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

        mainBtn = (Button)findViewById(R.id.button1);
        historyBtn = (Button)findViewById(R.id.button2);
        logoutBtn = (Button)findViewById(R.id.button3);
        // Register your buttons with the click listener
        mainBtn.setOnClickListener(this);
        historyBtn.setOnClickListener(this);
        logoutBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.button1:
                Intent main = new Intent (this, MainActivity2.class);
                startActivity(main);
                break;
            case R.id.button2:
                Intent history = new Intent(this, HistoryActivity.class);
                startActivity(history);
                break;
            case R.id.button3:
                Intent logout = new Intent(this, LoginActivity.class);
                keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(logout);
                this.finish();
        }
 }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_menu, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
 }


}

答案 3 :(得分:0)

您没有为按钮设置监听器。

mainBtn = (Button)findViewById(R.id.button1);
historyBtn = (Button)findViewById(R.id.button2);
logoutBtn = (Button)findViewById(R.id.button3);

mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);

祝你好运。 :)

答案 4 :(得分:0)

mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);