在没有onCreate {}的情况下开始意图 [英] start intent without onCreate {}

查看:205
本文介绍了在没有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 ActivityReference 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屋!

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