【问题标题】:Error:(124, 62) error: incompatible types: Class cannot be converted to Context错误:(124, 62) 错误:不兼容的类型:无法将类转换为上下文
【发布时间】:2016-01-01 18:37:16
【问题描述】:

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

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

错误

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

我更改为 EditInformation.getActivity(),但收到错误 non-static method

【问题讨论】:

  • 只是 getActivity()....

标签: java android android-fragments fragment


【解决方案1】:

改变

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

到

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

既然您已经在 Fragment 上下文中,那么 getActivity() 应该可以解决问题。

【讨论】:

    【解决方案2】:

    当你在一个片段中时,你不会通过说 YourFragment.this 来获得上下文。为此,您可以使用 getActivity() 方法,该方法将返回容器活动的上下文,您也可以说 getActivity().getApplicationContext() 两者都可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2023-01-18
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多