-2

I'm trying to create alertdialogs through a custom dialogframent class

public class Dialogs extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle saveInstanceState) {
        AlertDialog.Builder alert=new AlertDialog.Builder(getActivity());

        //additional custom layout here(skipped)

        alertDialog=alert.create();
        return alert.create();
    }

    public static Dialogs newInstance(String title, String fields, String buttons) {
        Dialogs d = new Dialogs();
        Bundle args = new Bundle();
        args.putString(TITLE,title);
        args.putString(FIELDS, fields);
        args.putString(BUTTONS,buttons);
        d.setArguments(args);
        return d;
    }

    public List<Button> getButtons() {
        return this.buttons;
    }

    public AlertDialog getDialog(){
        return alertDialog;
    }

I am calling it in my main activity via

public void AddNewTask() {
    Dialogs my_dialog = Dialogs.newInstance("Add New Task",NEW_TASK,NEW_TASK_BUTTON);
    my_dialog.getDiag().show();
}

getting

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlertDialog.show()' on a null object reference

I have a hunch it's that the arguments were put after the d was created. So the argumnets didn't pass through? Any help is appreciated

Edit: This is not a question of what is a nullpointer exception. I understand very well what it is. I have solve the problem for those that possibly have similar issues in the future. I changed the way the dialog was launched inside main activity. It seems like I was missing a fragmenttransaction.

 final Dialogs my_dialog=Dialogs.newInstance("Add New Task", NEW_TASK, NEW_TASK_BUTTON);
        FragmentTransaction  ft = getFragmentManager().beginTransaction();
        my_dialog.show(ft, "dialog");
Nych
  • 78
  • 1
  • 3
  • 10

2 Answers2

-1

You are set a Dialog inside a Dialog. Is that really what you want? I don't think so. Extend the Dialog and override its methods. Don't forget to pass the Context, so you are able to resolve resources (such as Strings, images, colors, etc).. I made a little lib for that, feel free to use it or just take a look to get a better understanding. MaterialDialog

(Please note that the lib is only providing basic functionality and probably there are some bugs)

Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
-1

You can not put getActivity() . It won't work. YOu need to pass activity object

Here is my code snippet:

public class DialogRating extends DialogFragment implements View.OnClickListener {

static BaseActivity activity;
String outletId;
String rating;


public DialogRating() {
}

public static DialogRating newInstance(BaseActivity activity, String outletId, String rating) {
    DialogRating.activity = activity;

    DialogRating fragment = new DialogRating();
    Bundle args = new Bundle();
    args.putString(Const.EXTRA_ID, outletId);
    args.putString(Const.EXTRA_RATING, rating);
    fragment.setArguments(args);
    return fragment;
}}
Irfan Raza
  • 2,859
  • 1
  • 22
  • 29