java – MainActivity. This is not a closed class asynctask

I tried to create an asynctask for the first time, but I didn't have much luck

My asynctask needs to get some information from the server and then add a new layout to the main layout to display this information

Everything seems more or less clear, but the error message "mainactivity is not a closed class" bothers me

No one seems to have this problem, so I think I miss something obvious. I just don't know what it is

In addition, I don't know if I get the context in the right way, and because my application doesn't compile, I can't test it

Thank you very much for your help

This is my 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;
        for(Card card : cards)// Amount of "cards" can be different each time
        {
            //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));
        }
    }
}

Solution

Eclipse may be right. You are trying to access the class (mainactivity) in its own file from another class belonging to its own file (backgroundworker) There is no way to do this - how should a classroom magically know each other's examples? What can you do:

>Move asynctask, so it is an inner class in mainactivity. Pass your activity to asynctask (through its constructor), and then use activityvariable findViewById(); (I used m activity in the following example) or, your ApplicationContext (using the correct naming convention, a needs to be lowercase) is actually an instance of a mainactivity. You can do it well, so ApplicationContext findViewById();

Examples of using constructors:

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

   public BackgroundWorker (Activity activity)
   {
     super();
     mActivity = activity;
   }

//rest of code...

as for

don't worry.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>