MainActivity.this is not an enclosing class AsyncTask(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 usingmActivity
in the example below) Alternatively, yourApplicationContext
(use proper naming convention, theA
needs to be lowercase) is actually an instance ofMainActivity
you're good to go, so doApplicationContext.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MainActivity.this 不是封闭类 AsyncTask
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01