I'm trying to fire an event when an integer value is updated, but it's failing. Here's the code I'm using:
Declaring The Custom Listener
public class fieldactivity extends AppCompatActivity implements View.OnClickListener {
OnModeUpdate modeupdate; //Create custom listener for mode update
int mode = 1;
Mode Update Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fieldsignals);
Button button = (Button) findViewById(R.id.mode_rotate_button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case(R.id.rotate_button):
mode += 1;
modeupdate.onEvent(); //Fire Custom Lisentener - Fails On This Line
}
}
Interface Code
public interface OnModeUpdate {
//BreakPoint here, but is never reached
void onEvent();
}
public void setModeupdate(OnModeUpdate eventListener) {
modeupdate = eventListener;
}
The error I am getting is:
java.lang.NullPointerException: Attempt to invoke interface method 'void alveare.com.plcsignalconverter.fieldactivity$OnModeUpdate.onEvent()' on a null object reference
Is there something I am missing?
The NullPointerException is caused by calling methods on a null referenc object, which is an object that has not been initialized.
In your case, the null object is modeUpdate
. Try to initialize it in the onCreate()
of your activity.
modeupdate = new OnModeUpdate() {
@Override
public void onEvent() {
/**
* Write the code to handle the case
*/
}
};
More general name for listener is observer * design pattern. Speaking the pattern terminology what you're trying to achieve is to make an observer, i.e modeupdate
, get notified when an event (integer value change) occurrs within an observable object, i.e.fieldactivity
interacting with a user.
The observer, modeupdate
, may be of any type and must implement the OnModeUpdate
interface in order to receive notifications from the observable object. E.g.**
public class CustomListener implements OnModeUpdate {
@Override
public void onEvent() {
...
}
}
Now you can assign the observer (modeupdate
) within the observable object (fieldactivity
) by one of the following options (according to your business logic):
initialize
modeupdate
with an "external" class instanceOnModeUpdate modeupdate = new CustomListener();
initialize
modeupdate
with an anonymous inner class instance withinfieldactivity
classmodeupdate = new OnModeUpdate() { @Override public void onEvent() { // Do stuff regarding the mode change } };
assign
modeupdate
from outside of thefieldactivity
class by callingsetModeupdate(...)
on the reference tofieldactivity
instance ***.
Above said explains the cause of NullPointerException
. You never initialized modeupdate
. After the initialization of modeupdate
the onEvent()
method will be triggered on a button click.
Note: you wrote in the comment to your code: "... BreakPoint here, but is never reached." It cannot be reached within the interface declaration, only on an instance of the object that implements the interface.
* Another name of the pattern is Subscriber-Publisher pattern.
** If OnModeUpdate interface is declared within fieldactivity use public class CustomListener implements fieldactivity.OnModeUpdate
*** Same as you did with the button variable by calling setOnClickListener() outside of the Button class and assigning a listener, i.e. an object (fieldactivity) that implements View.OnClickListener interface
来源:https://stackoverflow.com/questions/39948709/android-custom-listener-for-an-event