簡體   English   中英

上下文無法解析為類型

[英]Context cannot be resolved to a type

幾天前,我開始研究Android開發。 我正在嘗試從REST API提取數據,並且正在使用AsyncTask類進行HTTP事務。 問題是,我無法獲取我的主要活動的上下文以便找到我的ListView並在onPostExecute()中為其分配內容。

這是MainActivity.java:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new DownloadMediaList(this).execute();
    }
}

這是DownloadMediaList.java:

public class DownloadMediaList extends AsyncTask<Void, Void, ArrayList<Media>> {

    ListView listView = null;
    Context mainContext = null;

    public DownloadMediaList(Context main){
        this.mainContext = main;
    }

    @Override
    protected void onPreExecute(){
        listView = (ListView) mainContext.this.findViewById(R.id.media_list);
    }

    // Operations that we do on a different thread.
    @Override
    protected ArrayList<Media> doInBackground(Void... params){
        // Set an ArrayList to store the medias.
        ArrayList<Media> mediaList = new ArrayList<Media>();

        // Call the REST API and get the request info and media list in a JSONObject.
        RESTFunctions restRequest = new RESTFunctions();
        JSONObject jsonMedia = restRequest.getMediaList();

        // Try catch to catch JSON exceptions.
        try {
            // Store the media list into a JSONArray.
            JSONArray mediaArray = jsonMedia.getJSONArray("media");

            // Create an instance of media to store every single media later.
            Media media = new Media();

            // Loop through the JSONArray and add each media to the ArrayList.
            for (int i=0; i<mediaArray.length();i++){
                media = new Media();
                JSONObject singleMedia = mediaArray.getJSONObject(i);
                media.setTitle(singleMedia.getString("titre"));
                media.setYear(singleMedia.getString("annee"));
                media.setLength(singleMedia.getString("duree"));
                mediaList.add(media);
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Return the ArrayList.
        return mediaList;
    }

    // Operations we do on the User Interface. Synced with the User Interface.
    @Override
    protected void onPostExecute(ArrayList<Media> mediaList){
        // Set TextViews in ListViews here
    }
}

這些類位於兩個單獨的文件中。

這行特別給我帶來麻煩:

listView = (ListView) mainContext.this.findViewById(R.id.media_list);

我究竟做錯了什么? 它告訴我,即使我已導入android.content.Context,也無法將Context解析為一種類型。 我嘗試實例化Context,但是我也不能這樣做。

Context沒有視圖,也沒有findViewById方法-嘗試使您的mainContext類型為Activity

更換

listView = (ListView) mainContext.this.findViewById(R.id.media_list);

listView = (ListView) mainContext.findViewById(R.id.media_list);

錯誤應該消失。

您可以改為將AsyncTask作為內部類添加到您的活動中。

public class MainActivity extends Activity {
  private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    new DownloadMediaList(this).execute();
}

private class DownloadMediaList extends AsyncTask<Void, Void, ArrayList<Media>> {

ListView listView = null;




@Override
protected void onPreExecute(){
    listView = (ListView) context.findViewById(R.id.media_list);
}

// Operations that we do on a different thread.
@Override
protected ArrayList<Media> doInBackground(Void... params){
    // Set an ArrayList to store the medias.
    ArrayList<Media> mediaList = new ArrayList<Media>();

    // Call the REST API and get the request info and media list in a JSONObject.
    RESTFunctions restRequest = new RESTFunctions();
    JSONObject jsonMedia = restRequest.getMediaList();

    // Try catch to catch JSON exceptions.
    try {
        // Store the media list into a JSONArray.
        JSONArray mediaArray = jsonMedia.getJSONArray("media");

        // Create an instance of media to store every single media later.
        Media media = new Media();

        // Loop through the JSONArray and add each media to the ArrayList.
        for (int i=0; i<mediaArray.length();i++){
            media = new Media();
            JSONObject singleMedia = mediaArray.getJSONObject(i);
            media.setTitle(singleMedia.getString("titre"));
            media.setYear(singleMedia.getString("annee"));
            media.setLength(singleMedia.getString("duree"));
            mediaList.add(media);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Return the ArrayList.
    return mediaList;
}

// Operations we do on the User Interface. Synced with the User Interface.
@Override
protected void onPostExecute(ArrayList<Media> mediaList){
    // Set TextViews in ListViews here
}
  }}

在您的活動課中

    public interface TheInterface {
public void theMethod(ArrayList<Media> result);

 }
     }

然后

    DownloadMediaList task = new DownloadMediaList (MainAactivity.this,new TheInterface() {
             @Override
             public void theMethod(ArrayList<Media> result) {
                // result available here do whatever with list media
            }  
        }); 

然后在AsyncTask中

Context mainContext = null;
TheInterface mlistener;
public DownloadMediaList(Context main,TheInterface listener){
    this.mainContext = main;
    mlistener = listener; 
}

然后在onPostExecute

 @Override
protected void onPostExecute(ArrayList<Media> mediaList){
   if (mlistener != null) 
    {
         mlistener.theMethod(mediaList);
    }
} 

編輯:

作為備選。

您可以在asycntask中定義接口,並讓您的活動類實現該接口。

暫無
暫無

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

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