简体   繁体   中英

Android DialogFragment accessing external class

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();
        }
    }
}

从对话框片段中删除静态关键字

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM