I have created a custom dialog in my app, and I want to add button click observables like so:
public class BasicDialog extends android.app.Dialog {
@BindView(R.id.button1)
TextView button1;
@BindView(R.id.button2)
TextView button2;
public BasicDialog(Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_basic);
...
ButterKnife.bind(this);
}
public Observable<Object> button1Clicks() {
return RxView.clicks(button1);
}
public Observable<Object> button2Clicks() {
return RxView.clicks(button2);
}
}
I then create my dialog and subscribe to button 1 clicks in my activity like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
BasicDialog basicDialog = new BasicDialog(this);
basicDialog.button1Clicks()
.subscribe(__ -> doStuff());
}
This results in a null pointer exception, as button1
is null at the point of subscription.
I know a possible fix to this would be to use a PublishSubject
in the dialog instead of trying to subscribe directly to the button click observable, but this seems like a very inelegant solution.
Thanks in advance for any suggestions!