在没有onCreate {}的情况下开始意图 [英] start intent without onCreate {}
问题描述
我创建了一个扩展Gallery的类.超级类中没有onCreate()方法,并且我无法运行我的意图. 这是我的示例代码:
I have created a class which extends Gallery. There is no onCreate() method in super Class, and I'm unable to run my intent. This is my sample code:
this.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent intent = new Intent(v.getContext(), ClassName.class);
startActivity(intent);
}}
以下尝试也无效:
Intent intent = new Intent(ThisClassName.this, ClassName.class);
startActivity(intent);
任何建议将不胜感激:)
Any advice would be greatly appreciated :)
推荐答案
实际上,startActivity();
是 Activity 类的方法,要运行此方法,您必须Context of Activity
或Reference of Activity
,您不能在没有Activity引用的情况下在Activity范围之外的其他类中运行此方法.
Actually, startActivity();
is the method of Activity class, To run this method you have to Context of Activity
or Reference of Activity
, You can not run this method in other class as outside of Activity scope without having Activity reference for it.
尝试
<Activity_Name>.this.startActivity(intent);
或
mContext.startActivity(intent);
此处 mContext 是您的Activity类的引用.
Here mContext is reference of your Activity class.
这篇关于在没有onCreate {}的情况下开始意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!