簡體   English   中英

錯誤:(124,62)錯誤:不兼容的類型:類無法轉換為上下文

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

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

更改

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

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

由於您已經處於Fragment上下文中,因此getActivity()應該可以解決問題。

當你在一個片段中時,你不會通過說YourFragment.this獲得上下文。 要實現這一點,你可以使用getActivity()方法返回容器活動的上下文,你也可以說getActivity()。getApplicationContext()都可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM