Why Can't Android:onclick Recognize The Specfied Function?
Solution 1:
You addanote()
function must be public
, not protected
:
publicvoidaddanote(View v)
Docs: https://developer.android.com/guide/topics/ui/controls/button#java
Solution 2:
As @Marcin mentioned, you should use public
instead of protected
. Also, you can use a different but better approach, add a listener on the Button
:
Add an id
attribute to the Button, then define the listener in the onCreate()
method.
XML:
<Button
android:id="addNote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:text="Add Note"/>
Then Java:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
...
Button addNote = findViewById(R.id.addNote);
addNote.setOnClickListner(newView.OnClickListener() {
@OverridepublicvoidonClick() {
//Your code to be implemented when button is clicked.
}
});
...
}
Solution 3:
You need to make the method public as mentioned here
https://developer.android.com/guide/topics/ui/controls/button#java
The method you declare in the android:onClick attribute must have a signature exactly as shown above. Specifically, the method must: Be public Return void Define a View as its only parameter (this will be the View that was clicked)
the code is working fine after making it public
Post a Comment for "Why Can't Android:onclick Recognize The Specfied Function?"