【问题标题】:android- calling Intent from an Inner classandroid- 从内部类调用 Intent
【发布时间】:2010-07-09 11:47:50
【问题描述】:

我想从一个内部类中调用一个新活动,该内部类在 d 类中定义,该类扩展了 Activity.... 用该 Inner 类的方法之一编写的部分是::

Intent 意图 = new Intent(this, Test2.class); 开始活动(意图);

Test2 与我的主类放置在同一个包中,并且 Eclipse 向我显示错误“构造函数 Intent(test.MyTimer, Class) 未定义”.......

解决办法是什么??

【问题讨论】:

    标签: android class android-activity android-intent


    【解决方案1】:

    只需像这样使用MyActivity.this

    Intent i = new Intent(MyActivity.this, MyActivity.class);
    

    【讨论】:

      【解决方案2】:

      我会将父级传递给构造函数中的 MyTimer 类,然后您可以将其传递给 Intent。 Intent 需要一个派生自 Context 的类。

      所以你的 MyTimer 可能看起来像

      public class MyActivity extends Activity
      {
          private void StartTimer()
          {
              MyTimer timer = new MyTimer(this);
              timer.startIntent();
          }
      
          private class MyTimer
          {
              private Activity _context;
              public MyTimer(Activity c)
              {
                  _context = c;
              } 
              public void startIntent()
              {
                Intent i = new Intent(_context, MyActivity.class);
                _context.startActivity(i);
              }
          }
      }
      

      希望对您有所帮助。

      【讨论】:

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