简体   繁体   中英

how to use the startActivity in a class that extends a view?

I have a Class PuzzleView that extends View. I want to be able to open a new activity Congratulations from this View when an if statement returns true.

public class PuzzleView extends View { 
/*Code here blah blah blah then*/
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) 
{
  if (game.isSolved()== true)
  {
    Context context = getContext();
    Intent i = new Intent(PuzzleView.this, Congratulations.class);
    getContext().startActivity(i);
  }

The error I am getting is "The constructor Intent(PuzzleView, Class) is undefined" here

new Intent(PuzzleView.this, Congratulations.class);

In reply to @imrankhan here is the whole onKeyDown()

@Override
   public boolean onKeyDown(int keyCode, KeyEvent event) 
   {

       if (game.isSolved()== true)
       {
           Context context = getContext();
           Intent i = new Intent(context, Congratulations.class);
           context.startActivity(i);
       }
       else
       {
      Log.d(TAG, "onKeyDown: keycode=" + keyCode + ", event="
            + event);
      switch (keyCode) {
      case KeyEvent.KEYCODE_DPAD_UP:
         select(selX, selY - 1);
         break;
      case KeyEvent.KEYCODE_DPAD_DOWN:
         select(selX, selY + 1);
         break;
      case KeyEvent.KEYCODE_DPAD_LEFT:
         select(selX - 1, selY);
         break;
      case KeyEvent.KEYCODE_DPAD_RIGHT:
         select(selX + 1, selY);
         break;
      case KeyEvent.KEYCODE_0:
      case KeyEvent.KEYCODE_SPACE: setSelectedTile(0); break;
      case KeyEvent.KEYCODE_1:     setSelectedTile(1); break;
      case KeyEvent.KEYCODE_2:     setSelectedTile(2); break;
      case KeyEvent.KEYCODE_3:     setSelectedTile(3); break;
      case KeyEvent.KEYCODE_4:     setSelectedTile(4); break;
      case KeyEvent.KEYCODE_5:     setSelectedTile(5); break;
      case KeyEvent.KEYCODE_6:     setSelectedTile(6); break;
      case KeyEvent.KEYCODE_7:     setSelectedTile(7); break;
      case KeyEvent.KEYCODE_8:     setSelectedTile(8); break;
      case KeyEvent.KEYCODE_9:     setSelectedTile(9); break;
      case KeyEvent.KEYCODE_ENTER:
      case KeyEvent.KEYCODE_DPAD_CENTER:
         game.showKeypadOrError(selX, selY);
         break;
      default:
         return super.onKeyDown(keyCode, event);
      }
      return true;
       }


   }

try as

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) 
{
  if (game.isSolved()== true)
  {
    Context context = getContext();
    Intent i = new Intent(context, Congratulations.class);
    context.startActivity(i);

  }
 return super.onKeyDown(keyCode, event);    
}

Why don't you try this -

Context context = getContext();
Intent i = new Intent(context, Congratulations.class);
context.startActivity(i);

You need to pass activity reference in Constructor of the view, you can use this reference later. So define a constructor like following:

public class PuzzleView extends View
{
    Activity mActivity;
    public PuzzleView(Activity activity, Context context)
    {
         super(context);
         this.mActivity=activity;
    }
}

now use this activity reference to start activity:

Intent i = new Intent(mActivity, Congratulations.class);
    getContext().startActivity(i);

I agree with the other answers here. But it looks like they lack information as of why it is not allowed. Let me put it in simple terms.

When you start an Intent, it requires two arguments, one would be the context or an Activity reference and the other would be the next Activity class.

But what you are trying to do is that, you are trying to pass the this reference to a class which extends View and which is why you are getting this error.

The context object actually acts as the reference in the stack as of from where the control was transferred. So it is required for you to pass the context object or the Activity object itself along with it.

Last but not least, Activity is actually extended form Context Class. So either of them will do the task for you.

You need to keep application context in the view class so following code will answer you.

 public class PuzzleView extends View { 
    Context mContext;
        public PuzzleView (Context context, AttributeSet attrs) {
        super(context, attrs);  
        mContext=context;// keep the context

        } 
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (game.isSolved()== true){
              Intent i = new Intent(mContext, Congratulations.class);
               mcontext.startActivity(i);
      }

EDIT

Your view should be like this.

<com.yourpackage.PuzzleView  
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/yourid"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM