Having the same issue. The button is initialized at the right time but for some reason, it crashes on click. The xml file says the onClick handler is missing the related activity.
xml
<Button
android:id="@+id/start_reg_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="216dp"
android:onClick="onClick"
android:text="@string/need_a_new_account" />
Java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
mRegBtn = (Button) findViewById(R.id.start_reg_btn);
mRegBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent reg_intent = new Intent(StartActivity.this, RegisterActivity.class);
startActivity(reg_intent);
}
});
}
Very new to android programming. Any help is appreciated
There are two ways of solving your issue:
In your case, you have declared an onClick handler for your button on XML with this line:
android:onClick="onClick"
So, you now have to create a method name onClick() in your activity and do your code there like this:
public void onClick(View v) {
Intent reg_intent = new Intent( StartActivity.this, RegisterActivity.class);
startActivity(reg_intent);
}
android:onClick="onClick"
and do what you did already:
mRegBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent reg_intent = new Intent( StartActivity.this, RegisterActivity.class);
startActivity(reg_intent);
}
});
So, the main concept is this you can't use android:onClick
and setOnClickListener
together. You have to use one at a time.
android:onClick and `setOnClickListener` can not used simultaneous.
To use android:onClick correctly, you could refer to the official link
Try getting current context using getContext()
and implement as.
public void onClick(View v) {
Intent reg_intent = new Intent(v.getContext(), RegisterActivity.class);
v.getContext().startActivity(reg_intent);
}
And either define onClick
from xml file or define it programmatically using View.setOnClickListener
. Both can not co-exist.
Just Remove onClick from your Xml and try to run once again
Found the error that the TextEditinput.= TextInputLayout and that's why it was crashing. It was also working either by the onClick Method or SetuponClickListener so thank you for everyone and their help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.