【问题标题】:start intent without onCreate {}在没有 onCreate {} 的情况下启动意图
【发布时间】:2012-06-27 09:10:18
【问题描述】:

我创建了一个扩展 Gallery 的类。超类中没有 onCreate() 方法,我无法运行我的意图。 这是我的示例代码:

 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);

任何建议将不胜感激:)

【问题讨论】:

  • 好的,只需创建一个方法并将意图放入方法中。然后创建一个类的实例并调用该方法。但请记住在 try, catch 方法中说唱。这应该工作
  • 哦,好吧,所以如果意图没有调用任何其他 Activity,那么只需创建一个瞬间并调用该方法,正如@herom 所说,您不能有意图地调用该类。

标签: android android-intent oncreate


【解决方案1】:

其实startActivity();Activity类的方法,要运行这个方法你必须Context of Activity或者Reference of Activity,你不能在Activity之外的其他类中运行这个方法没有 Activity 引用的范围。

试试,

<Activity_Name>.this.startActivity(intent);

mContext.startActivity(intent);

这里 mContext 是您的 Activity 类的引用。

【讨论】:

    【解决方案2】:

    正如你所写,你想启动一个Activity,如果你的类不继承自Activity(我猜是因为否则你必须实现onCreate())你将无法启动它完全有一个意图....

    【讨论】:

      【解决方案3】:

      如果你的类是主要活动的扩展,并且你想从类的主体调用其他活动,只需简单地在其他类中创建一个方法,然后通过创建该类的实例来调用该方法并调用方法。

      由于它们具有相同的性质,这将起作用,但请记住在 try 和 catch 方法中调用方法,否则可能会出错。

      【讨论】:

        【解决方案4】:

        Gallery 是一个View 类型,如果你会看到它的Hirerchy 你会看到它的super 类是View 并且视图没有任何onCreate 方法

        使用您的CustomGallery

        您可以从 xml 或代码中使用它

        <com.mypackage.CustomGallery >
        android:id...
        
        
         </com.mypackage.CustomGallery>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多