MainActivity.this ist keine einschließende Klasse AsyncTask

Ich versuche zum ersten Mal, eine AsyncTask zu erstellen, aber ich habe nicht viel Glück.

Meine AsyncTask muss einige Informationen von einem Server abrufen und dann dem Hauptlayout neue Layouts hinzufügen, um diese Informationen anzuzeigen.

Alles scheint mehr oder weniger klar zu sein, aber die Fehlermeldung "MainActivity ist keine einschließende Klasse" stört mich.

Niemand scheint dieses Problem zu haben, also vermisse ich etwas sehr Offensichtliches, ich weiß nur nicht, was es ist.

Ich bin mir auch nicht sicher, ob ich den richtigen Weg gewählt habe, um den Kontext zu ermitteln, und weil meine Anwendung nicht kompiliert wird, kann ich sie nicht testen.

Deine Hilfe ist sehr Willkommen.

Hier ist mein Code:

public class BackgroundWorker extends AsyncTask<Context, String, ArrayList<Card>> {
    Context ApplicationContext;

    @Override
    protected ArrayList<Card> doInBackground(Context... contexts) {
        this.ApplicationContext = contexts[0];//Is it this right way to get the context?
        SomeClass someClass = new SomeClass();

        return someClass.getCards();
    }

    /**
     * Updates the GUI before the operation started
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    /**
     * Updates the GUI after operation has been completed
     */
    protected void onPostExecute(ArrayList<Card> cards) {
        super.onPostExecute(cards);

        int counter = 0;
        // Amount of "cards" can be different each time
        for (Card card : cards) {
            //Create new view
            LayoutInflater inflater = (LayoutInflater) ApplicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewSwitcher view = (ViewSwitcher)inflater.inflate(R.layout.card_layout, null);
            ImageButton imageButton = (ImageButton)view.findViewById(R.id.card_button_edit_nickname);

            /**
             * A lot of irrelevant operations here
             */ 

            // I'm getting the error message below
            LinearLayout insertPoint = (LinearLayout)MainActivity.this.findViewById(R.id.main);
            insertPoint.addView(view, counter++, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage