该类型的方法 startActivity(Intent) 未定义?
所以我试图通过按一个小部件来启动一个活动。我不断遇到错误“照片类型的方法 startActivity(Intent) 未定义”,非常感谢任何帮助!我的班级代码如下:
package com.natehoch96.widgets.iOS;
import android.appwidget.AppWidgetProvider;
import android.content.Intent;
public class Photos extends AppWidgetProvider {
Intent myIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
{startActivity(myIntent); }
}
So im trying to start an activity by pressing a widget. I keep running into the error "The method startActivity(Intent) is undefined for the type Photos" any help is much appreciated! My class code is below:
package com.natehoch96.widgets.iOS;
import android.appwidget.AppWidgetProvider;
import android.content.Intent;
public class Photos extends AppWidgetProvider {
Intent myIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
{startActivity(myIntent); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 AppWidgetProvider 类中实现 onUpdate() 和 onReceive() 方法,如下所示:
然后编写 onReceive() 方法来接收挂起的意图并启动相关活动:
You need to implement the onUpdate() and onReceive() methods in your AppWidgetProvider class, something like this:
Then write your onReceive() method to receive the pending intent and start the relevant activity:
你用什么方法把这段代码放进去的?
AppWidgetProvider
没有startActivity()
方法。为此,您需要一个Context
。多个AppWidgetProvider
的回调方法可让您访问Context
对象,但这实际上完全取决于您打算执行的操作。我建议您在此处。
What method have you put this code in?
AppWidgetProvider
does not have astartActivity()
method. You need aContext
for that. Several ofAppWidgetProvider
's callback methods give you access to aContext
object, but it all really depends on what you intend to do.I suggest you take a look at the Android developer guide's documentation on AppWidgets here.