Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Button OnClickListener does not working

I have created this activity that should allow me to open a new activity once a button has been pressed.

However the OnClickListener does not seem to be working.

Am I declaring the buttons wrong?

Can someone me out?

public class Menu extends Activity {


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

    View loginbutton = findViewById(R.id.butlogin);
    loginbutton.setOnClickListener(this);

    View recordbutton = findViewById(R.id.butrecordts);
    recordbutton.setOnClickListener(this);

    View viewbutton = findViewById(R.id.butviewts);
    viewbutton.setOnClickListener(this);

    View projectsbutton = findViewById(R.id.butprojects);
    projectsbutton.setOnClickListener(this);

}

public void onClick(View v){

    switch(v.getId())
    {
    case R.id.butlogin:
    {
        //open login screen
        Intent i = new Intent(this, login.class);
        startActivity(i);
        break;
    }
    case R.id.butrecordts:
    {
        break;
    }
    case R.id.butviewts:
    {
        break;
    }
    case R.id.butprojects:
    {
        break;
    }

    }
}
like image 739
Hydar Roze Avatar asked Jan 09 '13 10:01

Hydar Roze


2 Answers

Yes, The Problem is in Declaration of button, write below code instead of your code, it will solve your problem.

public class Menu extends Activity implements OnClickListener{

    Button loginbutton, recordbutton, viewbutton, projectsbutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

        loginbutton = (Button) findViewById(R.id.butlogin);
        loginbutton.setOnClickListener(this);

        recordbutton = (Button) findViewById(R.id.butrecordts);
        recordbutton.setOnClickListener(this);

        viewbutton = (Button) findViewById(R.id.butviewts);
        viewbutton.setOnClickListener(this);

        projectsbutton = (Button) findViewById(R.id.butprojects);
        projectsbutton.setOnClickListener(this);

    }

    public void onClick(View v){

        switch(v.getId())
        {
            case R.id.butlogin:
            {
                //open login screen
                Intent i = new Intent(this, login.class);
                startActivity(i);
                break;
            }
            case R.id.butrecordts:
            {
                break;
            }
            case R.id.butviewts:
            {
                break;
            }
            case R.id.butprojects:
            {
                break;
            }

        }
    }
}
like image 82
Dipak Keshariya Avatar answered Nov 03 '22 00:11

Dipak Keshariya


You forget to implement onClickListener in your Activity.

Implement it and then try :)

public class Menu extends Activity implements onClickListener
like image 42
AndroidLearner Avatar answered Nov 03 '22 01:11

AndroidLearner