Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么安卓系统中的按钮不起作用?_Android_Button_Switch Statement_Logout - Fatal编程技术网

Android 为什么安卓系统中的按钮不起作用?

Android 为什么安卓系统中的按钮不起作用?,android,button,switch-statement,logout,Android,Button,Switch Statement,Logout,我创建了一个菜单活动,有3个按钮:主按钮、历史按钮、注销按钮。 我不知道为什么所有的按钮都不工作,还有注销功能。 反正我不知道注销功能,是正确的功能还是错误的功能? 我是Android新手,任何帮助都将不胜感激 这是我的密码: public class MenuActivity extends ActionBarActivity implements View.OnClickListener { Button mainBtn,historyBtn,logoutBtn ;

我创建了一个菜单活动,有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);
        }
    }
你这样做了吗

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

i、 e.在代码或xml文件中为按钮注册onClickListener。

将listner设置为所有按钮,如

 mainBtn.setOnClickListener(this);

您需要向click listener注册这些按钮。将代码替换为以下内容

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


}

您没有为按钮设置侦听器

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

祝你工作顺利

为按钮注册监听器。它应该被声明吗,先生?你需要解释你的代码,否则大多数人不会理解他们应该如何使用它。
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);