开发者

android- calling Intent from an Inner class

开发者 https://www.devze.com 2023-01-06 02:35 出处:网络
i want to call a new activity from within an inner class which is defined in d class which 开发者_StackOverflowextends Activity....

i want to call a new activity from within an inner class which is defined in d class which 开发者_StackOverflowextends Activity.... the piece of written in one of the methods of that Inner class is::

Intent intent = new Intent(this, Test2.class); startActivity(intent);

Test2 is placed inside the same package as my main clas is placed and eclipse is showing me d error "The constructor Intent(test.MyTimer, Class) is undefined".......

what is the solution??


Just use MyActivity.this like so:

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


I'd pass the parent to the MyTimer class in the constructor then you can pass that to the Intent. The intent requires a class that derives from Context.

So your MyTimer could look like

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

Hope that helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消