Unable to use Dialog Widget
kshkrao3 opened this issue · comments
I have below code written in my fragment to use Dialog Widget, but it always throws NullPointerException
when I try to show dialog with dialog.show
Code within Fragment:
Dialog dialog = new Dialog(MyActivity.getLoginRegisterActivityInstance(), "Account locked!", result.getErrorMessage());
dialog.setCancelable(false);
dialog.setOnAcceptButtonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.replace(R.id.logincontent, new RegisterFragment(), null);
ft.commit();
}
});
ButtonFlat acceptButton = dialog.getButtonAccept();
acceptButton.setText("Ok");
ButtonFlat cancelButton = dialog.getButtonCancel();
cancelButton.setVisibility(View.GONE);
dialog.show(); //Exception here
What could be the possible reason?
I think that dialog.getButtonAccept() only can be invoked when the Dialog is already created so try to do it in the post() method, like this:
dialog.post(
new Runnable(){
@Override
public void run() {
ButtonFlat acceptButton = dialog.getButtonAccept();
acceptButton.setText("Ok");
ButtonFlat cancelButton = dialog.getButtonCancel();
cancelButton.setVisibility(View.GONE);
}
});
I'm not sure if you must to invoke dialog.show(); after that, but try it too.
I hope it helps you!
No, even though I just initialize and show it, it throws exception. On further investigation, I found the onCreate
method of Dialog.java
throws the exception. view
and backView
within the code returns null and exception is during setOnTouchListner
of backView
which is a NullPointerException
. Below is the piece of code within onCreate
in Dialog.java
file.
view = (RelativeLayout)findViewById(R.id.contentDialog); //null
backView = (RelativeLayout)findViewById(R.id.dialog_rootView);//null
backView.setOnTouchListener(new OnTouchListener() { //Exception here
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getX() < view.getLeft()
|| event.getX() >view.getRight()
|| event.getY() > view.getBottom()
|| event.getY() < view.getTop()) {
dismiss();
}
return false;
}
});
Any idea why?
The issue has been resolved and was caused probably because of including one more dialog library. I aint sure though. Now its working fine. But I hereby want to report one more issue regarding setCancelable
and setCanceledOnTouchOutside
, both aren't working. I want to avoid dialog close on outside click and tried setting above 2 to false, but it did not work. Is there anything I should try on this? I suspect because of backView.setOnTouchListener
if condition wherein its not checked whether cancelable
has been set to true
or false
.
We are working now on an update, i note also this issue to be resolved in the next version
Thanks much for taking this into consideration..