3

I am trying to start a second activity but I am getting an error on the code.

import android.os.Bundle;
import android.content.Intent;
import android.app.Activity;
import android.widget.Button;

import android.view.View;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;


public class MainActivity extends Activity
{
    private static final int ID_SPLASH = 1;
    private static final int ID_MAIN = 2;
    private static final int ID_PROFILE = 3;
    private static final int ID_PLAY = 4;
    private static final int ID_HELP = 5;


    @Override

    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setContentView(R.layout.main);
        ImageButton menuBtn = (ImageButton)findViewById(R.id.iBtn1);
        //Button example1Btn    = (Button) findViewById(R.id.btn1);
        ActionItem splashItem   = new ActionItem(ID_SPLASH, "Splash", getResources().getDrawable(R.drawable.ic_action_about));
        ActionItem mainItem     = new ActionItem(ID_MAIN, "Main", getResources().getDrawable(R.drawable.ic_content_undo));
        ActionItem profileItem  = new ActionItem(ID_PROFILE, "Profile", getResources().getDrawable(R.drawable.ic_social_person));
        ActionItem playItem     = new ActionItem(ID_PLAY, "Play", getResources().getDrawable(R.drawable.ic_hardware_gamepad));
        ActionItem helpItem     = new ActionItem(ID_HELP, "Help", getResources().getDrawable(R.drawable.ic_action_help));

        //use setSticky(true) to disable QuickAction dialog being dismissed after an item is clicked
        helpItem.setSticky(true);

        final QuickAction mQuickAction  = new QuickAction(this);

        mQuickAction.addActionItem(splashItem);
        mQuickAction.addActionItem(mainItem);
        mQuickAction.addActionItem(profileItem);
        mQuickAction.addActionItem(playItem);
        mQuickAction.addActionItem(helpItem);

        //setup the action item click listener
        mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
            @Override
            public void onItemClick(QuickAction quickAction, int pos, int actionId) {
                ActionItem actionItem = quickAction.getActionItem(pos);

                if (actionId == ID_SPLASH) {
                    Intent intent = new Intent(this, SplashActivity.class);
                    startActivity(intent);

                } else if (actionId == ID_MAIN) {
                    Toast.makeText(getApplicationContext(), "Main---> item selected", Toast.LENGTH_SHORT).show();
                } else if (actionId == ID_PROFILE) {
                    Toast.makeText(getApplicationContext(), "Profile---> item selected", Toast.LENGTH_SHORT).show();
                } else if (actionId == ID_PLAY) {
                    Toast.makeText(getApplicationContext(), "Play---> item selected", Toast.LENGTH_SHORT).show();
                } else if (actionId == ID_HELP) {
                    Toast.makeText(getApplicationContext(), "Help---> item selected", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), actionItem.getTitle() + " selected", Toast.LENGTH_SHORT).show();
                }
            }
        });

        mQuickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
            @Override
            public void onDismiss() {
                Toast.makeText(getApplicationContext(), "Ups..dismissed", Toast.LENGTH_SHORT).show();
            }
        });

        menuBtn.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                mQuickAction.show(v);
            }
        });

    }
}

It doesn't like everything from the new onward.

I felt that I followed the directions http://developer.android.com/training/basics/firstapp/starting-activity.html

StephanM
  • 733
  • 3
  • 22
  • 36
  • Please write down all your project code – Mr.Me Jan 31 '13 at 17:51
  • Near impossible to help you without more information about what error your getting. Might be you need to import Intent, your context is incorrect, your activity class doesn't exist etc. – Hrafn Jan 31 '13 at 17:55
  • Thats in another file, but the error is Remove arguments to match “intent()” – StephanM Jan 31 '13 at 17:58

2 Answers2

6

What your trying to pass in as a context is actually onActionItemClickListener() which will not work.

Should use getBaseContext().

Intent intent = new Intent(getBaseContext(), SplashActivity.class);

Edit: Or you could do

Intent intent = new Intent(MainActivity.this, SplashActivity.class);

Hrafn
  • 2,867
  • 3
  • 25
  • 44
  • I also just found this, not sure what I was googleing before that it didn't come up. http://stackoverflow.com/questions/8266355/startactivity-red-highlighted – StephanM Jan 31 '13 at 18:09
  • Almost right. onActionItemClickListener() is not a context. It's a class instance. Context is a base class in Android and Activity derives from it. – Simon Jan 31 '13 at 18:17
4

In Java, this refers to the current instance of the class containing the code.

Here

 mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
        @Override
        public void onItemClick(QuickAction quickAction, int pos, int actionId) {
            ActionItem actionItem = quickAction.getActionItem(pos);

            if (actionId == ID_SPLASH) {
                Intent intent = new Intent(this, SplashActivity.class);
                startActivity(intent);

you are creating the intent inside onItemClick which is a method of an instance of the class OnActionItemClickListener. This is known as an "anonymous inner class". Anonymous because it has no name (unlike public class MyClass) and inner because it exists only inside the class instance declaring it.

this therefore refers to the instance of the click listener.

Instead, use the instance of the outer class - your Activity.

Intent intent = new Intent(MainActivity.this, SplashActivity.class);

Another way of thinking about this is that the constructor for Intent that you are using expects a Context as the first parameter.

From the docs:

Intent(Context packageContext, Class cls) Create an intent for a specific component.

Since Activity extends Context, your activity is an instance of Context. OnActionItemClickListener is a class so the compiler will generate a compile time error.

Simon
  • 14,407
  • 8
  • 46
  • 61