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;
}
}
}
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;
}
}
}
}
You forget to implement onClickListener
in your Activity.
Implement it and then try :)
public class Menu extends Activity implements onClickListener
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With