John
John

Reputation: 694

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

Upvotes: 3

Views: 5931

Answers (2)

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.

Upvotes: 1

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17142

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.

Upvotes: 3

Related Questions