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

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

问题描述

我第一次尝试创建 AsyncTask,但运气不佳.

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

我的 AsyncTask 需要从服务器获取一些信息,然后将新布局添加到主布局以显示这些信息.

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

一切似乎或多或少都清楚了,但是MainActivity 不是封闭类"的错误消息困扰着我.

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.

非常感谢您的帮助.

这是我的代码:

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

推荐答案

Eclipse 可能是对的,而您正试图访问它自己的内部的类 (MainActivity)strong> 来自另一个类的文件,该文件位于其自己的文件 (BackgroundWorker) 中.没有办法做到这一点 - 一个班级如何神奇地知道另一个班级的实例?你可以做什么:

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:

  • 移动 AsyncTask 使其成为 内部 MainActivity
  • 中的类
  • 将您的 Activity 传递给 AsyncTask(通过其构造函数)然后使用 activityVariable.findViewById(); 访问(我在下面的示例中使用 mActivity)或者,你的 ApplicationContext(使用正确的命名约定,A 需要小写)实际上是 MainActivity 的一个实例,你很高兴,所以做 ApplicationContext.findViewById();
  • 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();

使用构造函数示例:

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

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

//rest of code...

至于

我不确定我是否使用了正确的方式来获取上下文

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

没问题.

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

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