简体   繁体   中英

Error:(124, 62) error: incompatible types: Class cannot be converted to Context

EditInformation extending to fragment. I get error in this line

loading = ProgressDialog.show(EditInformation.this,"Fetching...","Wait...",false,false); , wrong 1st argument type.

 public void RetrieveInformation(final String id)
    {
        class GetEmployee extends AsyncTask<Void,Void,String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(EditInformation.this,"Fetching...","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                showEmployee(s);
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s = rh.sendGetRequestParam(Config.RETRIEVE_INFORMATION,id);
                return s;
            }
        }
        GetEmployee ge = new GetEmployee();
        ge.execute();
    }

Error

 Error:(124, 62) error: incompatible types: EditInformation cannot be converted to Context

I change to EditInformation.getActivity(), but get error non-static method

Change

loading = ProgressDialog.show(EditInformation.this,"Fetching...","Wait...",false,false);

to

loading = ProgressDialog.show(getActivity(),"Fetching...","Wait...",false,false);

Since you're already in a Fragment context, getActivity() shall do the trick.

As you are in a fragment you will not get context by saying YourFragment.this . To achieve that you can use getActivity() method which will return context of container activity and also you can say getActivity().getApplicationContext() both will work fine.

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