MainActivity.this不是一个封闭类的AsyncTask [英] MainActivity.this is not an enclosing class AsyncTask

查看:2265
本文介绍了MainActivity.this不是一个封闭类的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造第一时间的AsyncTask,但我没有多少运气。

我的AsyncTask需要从服务器得到一些信息,然后添加新的布局,以主要布局以显示此信息。

一切似乎都或多或少清楚,但,错误消息MainActivity不是一个封闭类是困扰我。

没有其他人似乎有这个问题,所以我觉得我会错过一些很明显的,我只是不知道它是什么。

另外,我不知道如果我用正确的方式来获取上下文,因为我的应用程序无法编译,所以我不能对它进行测试。

您的帮助是多少AP preciated。

下面是我的code:

 公共类的BackgroundWorker扩展的AsyncTask<上下文,字符串的ArrayList<卡>>
{
    语境的ApplicationContext;    @覆盖
    保护的ArrayList<卡> doInBackground(上下文语境...)
    {
        this.ApplicationContext =上下文[0]; //是否得到背景下,这个正确的方式?        SomeClass的SomeClass的=新SomeClass的();        返回someClass.getCards();
    }    / **
     *更新GUI操作开始前
     * /
    @覆盖
    在preExecute保护无效()
    {
        super.on preExecute();
    }    @覆盖
    / **
     *更新的GUI操作已经完成之后
     * /
    保护无效onPostExecute(ArrayList的<卡>卡)
    {
        super.onPostExecute(卡);        INT计数器= 0;
        对于(卡牌:卡)//量的卡,每次都可以是不同的
        {
            //创建新视图
            LayoutInflater吹气=(LayoutInflater)ApplicationContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            ViewSwitcher观点=(ViewSwitcher)inflater.inflate(R.layout.card_layout,NULL);            的ImageButton的ImageButton =(的ImageButton)view.findViewById(R.id.card_button_edit_nickname);            / **
             *很多不相关的业务在这里
             * /            //我收到以下错误消息
            的LinearLayout在insertPoint =(的LinearLayout)MainActivity.this.findViewById(R.id.main);
            insertPoint.addView(视图,反++,新ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT));
        }
    }
}


解决方案

Eclipse的可能是正确的,而你试图访问一个类( MainActivity ),这是它里面的从另一个类是在它自己的文件文件(的BackgroundWorker )。有没有办法做到这一点 - 是一个类应该如何了解对方的情况下神奇?你能做什么:


  • 将在AsyncTask的所以它是一个 MainActivity

  • 冒充您的活动到AsyncTask的(通过其构造函数),然后接取使用 activityVariable.findViewById(); (我用 mActivity 在下面的例子)。另外,你的的ApplicationContext (使用正确的命名约定中, A 需要是小写)实际上是一个实例 MainActivity 你是好走,所以做 ApplicationContext.findViewById();

使用构造例如:

 公共类的BackgroundWorker扩展的AsyncTask<上下文,字符串的ArrayList<卡>>
{
    语境的ApplicationContext;
    活动mActivity;   公共BackgroundWorker的(活动活动)
   {
     超();
     mActivity =活动;
   }// code休息...

至于


  

我不知道如果我用正确的方式来获取上下文


这是好的。

I'm trying to create an AsyncTask for the 1st time, but I don't have much luck.

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

Everything seems to be more or less clear but, the error message "MainActivity is not an enclosing class" is bothering me.

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

Also, I'm not sure if I used the right way to get the context, and because my application doesn't compile so I can't test it.

Your help is much appreciated.

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

解决方案

Eclipse is probably right, and you are trying to access a class (MainActivity) that is inside it's own file from another class that is in its own file (BackgroundWorker) . There is no way to do that - how is one class supposed to know about the other's instance magically? What you can do:

  • Move the AsyncTask so it is an inner class in MainActivity
  • Pass off your Activity to the AsyncTask (via its constructor) then acess using activityVariable.findViewById(); (I am using mActivity in the example below) Alternatively, your ApplicationContext (use proper naming convention, the A needs to be lowercase) is actually an instance of MainActivity you're good to go, so do ApplicationContext.findViewById();

Using the Constructor example:

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

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

//rest of code...

As for

I'm not sure if I used the right way to get the context

It is fine.

这篇关于MainActivity.this不是一个封闭类的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆