OGeek|极客世界-中国程序员成长平台

标题: java - 错误 :(124, 62) 错误:不兼容的类型:无法将类转换为上下文 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-9 06:19
标题: java - 错误 :(124, 62) 错误:不兼容的类型:无法将类转换为上下文

EditInformation 扩展到 fragment 。我在这一行得到错误

loading = ProgressDialog.show(EditInformation.this,"Fetching...","Wait...",false,false);,第一个参数类型错误。

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

错误

 Error124, 62) error: incompatible types: EditInformation cannot be converted to Context

我改成EditInformation.getActivity(),但是报错non-static method



Best Answer-推荐答案


改变

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

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

由于您已经在 Fragment 上下文中,getActivity() 应该可以解决问题。

关于java - 错误 124, 62) 错误:不兼容的类型:无法将类转换为上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34558893/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://www.ogeek.cn/) Powered by Discuz! X3.4