Reputation: 331
I have created Dialog Fragment with custom layout and want to set text for some textboxes. But when trying to get view in my adapter image click I'm getting null pointer exception.
I would like to know is there any best way to implement it?
Thanks in advance.
Below is my code:
public class AlertDialogFragment extends DialogFragment
{
private View dialogView;
private static IDialogButtonsClicks buttonsClicks;
private AlertDialog.Builder alertDialog;
public static AlertDialogFragment newInstance(int title, int icon, int positive, int negative, int view, int message, IDialogButtonsClicks clicks)
{
AlertDialogFragment dialogFragment = new AlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
args.putInt("icon", icon);
args.putInt("pos", positive);
args.putInt("neg", negative);
args.putInt("view", view);
args.putInt("message", message);
buttonsClicks = clicks;
dialogFragment.setArguments(args);
return dialogFragment;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
alertDialog = new AlertDialog.Builder(getActivity());
int dView = getArguments().getInt("view");
int mess = getArguments().getInt("message");
if (dView != -1)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
dialogView = layoutInflater.inflate(dView, null);
alertDialog.setView(dialogView);
}
if (mess != -1)
{
alertDialog.setMessage(mess);
}
alertDialog.setTitle(getArguments().getInt("title"));
alertDialog.setIcon(getArguments().getInt("icon"));
alertDialog.setPositiveButton(getArguments().getInt("pos"), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
buttonsClicks.OnPositiveButtonClick(dialogView);
}
});
alertDialog.setNegativeButton(getArguments().getInt("neg"), new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
buttonsClicks.OnNegativeButtonClick();
}
});
return alertDialog.create();
}
public View getDialogView()
{
return dialogView;
}
@Nullable
@Override
public View getView()
{
return super.getView();
}
}
holder.imgCatEdit.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialogFragment editCatDialog = AlertDialogFragment.newInstance(R.string.add_category_title, R.drawable.ic_edit, R.string.edit_cat_button_save, R.string.edit_cat_button_cancel, R.layout.edit_category, -1, new IDialogButtonsClicks()
{
@Override
public void OnPositiveButtonClick(View view)
{
}
@Override
public void OnNegativeButtonClick()
{
}
});
editCatDialog.show(((MyBusinessMainActivity)baseContext).getSupportFragmentManager(), "EditCatDialog");
View dialogView = editCatDialog.getView();//It seems here I'm getting null
EditText ed_edit_cat_name = (EditText)dialogView.findViewById(R.id.ed_edit_cat_name);
EditText ed_edit_cat_descr = (EditText)dialogView.findViewById(R.id.ed_edit_cat_descr);
ed_edit_cat_name.setText(cat_name);
ed_edit_cat_descr.setText(cat_descr);
}
});
11-23 12:40:49.708 19702-19702/am.nkr.mybusiness E/AndroidRuntime: FATAL EXCEPTION: main
Process: am.nkr.mybusiness, PID: 19702
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at am.nkr.mybusiness.CategoriesAdapter$2.onClick(CategoriesAdapter.java:165)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Upvotes: 1
Views: 1792
Reputation: 8210
Yeah, you got that error because when you call ed_edit_cat_name.setText(cat_name);
, your DialogFragment
callback onCreateView
is not finished! To overcome this problem, I think you can declare some variables to hold your cat_name
and cat_des
in your DialogFragment
as below:
public class AlertDialogFragment extends DialogFragment
{
private String catName;// getter - setter
private String catDes;//getter-setter
// TODO: find your view and set cat name, description in onCreateView callback.
}
Then, you can set your cat name, cat des when you create new instance of DialogFragment
.
Upvotes: 1
Reputation: 3175
It is because when you do editCatDialog.getView();
the view have not been yet initializied (onCreateDialog()
was not called).
The best way would be to add listeners in dialog in onViewCreated
method. Then passing those events to your main fragment (e.g. via buttonClicks
variable).
If you want to make it in a quick and hacky way you could write getSupportFragmentManager().executePendingTransaction()
after show
method
Upvotes: 2