【问题标题】:Android : Network on main Thread with a WidgetAndroid:带有小部件的主线程上的网络
【发布时间】:2013-01-17 21:14:06
【问题描述】:

我做了一个简单的小部件 (AppWidgetProvider)。要加载它的内容,它需要连接到互联网(JSoup 太棒了)。显然,我有一个很棒的NetworkOnMainThreadException。我试图做一个AsyncTask,但 Eclipse 对我大喊No enclosing instance of type WidgetClass is accessible. Must qualify the allocation with an enclosing instance of type WidgetClass (e.g. x.new A() where x is an instance of WidgetClass).,所以我无法运行它。

我该如何解决?

谢谢!

【问题讨论】:

  • 您收到的具体错误消息是什么?也是 Eclipse 抱怨的那一行。还要考虑使用正确的 Java 命名约定 类名以大写字母开头;变量名没有。
  • Eclipse 绝对不想要 myAsync a = new myAsync().execute() 这样的东西,因为 new myAsync.execute() 可能不会返回 myAsync 的实例,除非您对其进行了编程。我相信你误会了什么。您介意提交您的代码吗?
  • 当我尝试new Background().execute(""); 时,我得到了No enclosing instance of type Widget is accessible. Must qualify the allocation with an enclosing instance of type Widget (e.g. x.new A() where x is an instance of Widget).

标签: android multithreading exception networking widget


【解决方案1】:

也许您正试图在声明区调用 myAsync().execute()?

public class MyActivity extends Activity {
    new myAsync().execute();//wouldn't work
}

【讨论】:

  • 不,它在 onUpdate 方法中。
  • 我做到了:当我尝试 new Background().execute("");我没有可以访问 WidgetClass 类型的封闭实例。必须使用 WidgetClass 类型的封闭实例来限定分配(例如 x.new A(),其中 x 是 WidgetClass 的实例)。 (我的课叫WidgetClass)
猜你喜欢
  • 2013-06-26
  • 1970-01-01
  • 2018-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
相关资源
最近更新 更多