0

I am trying to do a Dialog Fragment to access a external class.

In the Dialog Fragment I have this line: "new JSONParse().execute();" In this line, Android Studio IDE says that can not be referenced from a static context.

How could I change the code such as I can call JSONParse class in Dialog Fragment?

public class HorariosMedFragment extends Fragment {
...
 Button butMarca = (Button) view.findViewById(R.id.but_horarios_view);
 butMarca.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new ConfirmDialogFragment().show(getFragmentManager(),  "MyDialog");
            }
        });

   public class JSONParse extends AsyncTask<String, String, JSONObject> {

        @Override
        protected void onPreExecute() {
           ....
        }

        @Override
        protected JSONObject doInBackground(String... args) {
            .....
            return json;
        }

        @Override
        protected void onPostExecute(JSONObject json) {
           ....
        }
    }

    public static class ConfirmDialogFragment extends DialogFragment {
        Context mContext;
        public ConfirmDialogFragment() {
            mContext = getActivity();
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
            alertDialogBuilder.setTitle("Confirmação?");
            alertDialogBuilder.setMessage("Voce confirma o agendamento?");
            alertDialogBuilder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            new  JSONParse().execute(); //Here is my problem
                        }
                    });
            alertDialogBuilder.setNegativeButton("Não", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            return alertDialogBuilder.create();
        }
    }
}
Luiz Alves
  • 2,575
  • 4
  • 34
  • 75
  • Does both of your ConfirmDialogFragment and JSONParse classes are inner class of HorariosMedFragment? – fluffyBatman Oct 23 '16 at 18:28
  • @ fluffyBatman Yes, They do. – Luiz Alves Oct 23 '16 at 19:01
  • I have a workaround using: JSONParse js=new JSONParse() in HorariosMedFragment and in the DialogFragmet I did: ((HorariosMedFragment) getFragmentManager().findFragmentById(R.id.fragment_container)).js.execute(); It works, but I don´t know if it´s a good solution. I already had to change the original post to public ConfirmDialogFragment() { mContext = getContext(); } and AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext); – Luiz Alves Oct 23 '16 at 19:26

2 Answers2

0

remove static keyword from your dialog fragment

0

Ah, I had this problem recently. So you must create an instance of the object before you call execute. So replace Jsonparse.execute with two lines:

JsonParse parse = new JsonParse();

parse.execute();

This did it for me. Hope it helps!

endlesschaos
  • 145
  • 1
  • 6
  • It doesn´t works to me. I still have problem with HorariosMedFragment.this can not be referenced from static context – Luiz Alves Oct 23 '16 at 19:11