tags:

views:

2681

answers:

3

Hi,

Im new on Android, and im trying to start an Activity from a MenuItem choose of the user.

Actually, im building my menu (and is working ok) from my main activity class using a MenuInflater:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        super.onCreateOptionsMenu(menu);
        //the Menu Inflater class allows to create a menu from a XML File
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.layout.menutest,menu);
        return true;
    }

And im handling the Menu selection using the following code (working fine too):

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) 
{
    case R.id.MenuItemNewWebsite:
        ShowScreenAddSite();
    break;
    default:

    break;
}
return false;

}

I have a second and last activity called AddWebsite, and i would like to start it, but the following code doesnt work:

protected void ShowScreenAddSite()
{
    Intent i = new Intent(AddWebsite.class);
    startActivity(i);

}

Do you know what is the extra thing that i have to pass to the Intent constructor? Thanks in advance. Best Regards. Jose

+1  A: 

I'm still quite new to android myself but don't you need to be passing a context to the Intent constructor?

protected void ShowScreenAddSite()
{
    Intent i = new Intent(this, AddWebsite.class);
    startActivity(i);

}

You're probably doing this from inside an activity so I think you should be using 'this'

MattyW
Hi Matt, Thanks for the response. I tryied this option, but i get an error saying that the application has finished unexpectly.The code of my Addwebsite activity is the following (maybe there is anything wrong...):public class AddWebsite extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.screenaddsite); }}Do you see anything else weird in the code?Thanks.Best Regards.Jose.
Josemalive
+6  A: 

Hi,

the solution was too simple, appears that in android, every activity class is not automatically referenced in the manifest.xml.

I just only add the new activity to the manifest, and works fine.

Regards. Jose

Josemalive
Since this solved your problem you can accept your own answer.
Dave Webb
+1  A: 

You can also do something like this

/* (non-Javadoc)
 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    final MenuInflater inflater = new MenuInflater(this);
    final Intent[] menuIntents = new Intent[] {
              new Intent(this, AddWebsite.class) };
    inflater.inflate(R.menu.mymenu, menu);
    final int ms = menu.size();
    for (int i=0; i < ms; i++) {
        menu.getItem(i).setIntent(menuIntents[i]);
    }
    return super.onCreateOptionsMenu(menu);
}

avoiding some method calls, however you need to pay attention to the mapping between menu ids, menu order and intents but this is almost always known.

dtmilano