user2651180
user2651180

Reputation: 11

Opening new activity from menu

I'm writing app for android and I have some problems. I have main activity and settings activity. I want to start settings activity from main activity menu. I have this code to do this:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.menu_preferences:
        Intent j = new Intent(getApplicationContext(), SettingsActivity.class);
        startActivity(j);
        break;
    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

But it isn't working. When i'm trying it i have logcat:

Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40a254c8

And settings activity isn't showing. I don't know what to do...

Upvotes: 0

Views: 151

Answers (4)

Pontus Backlund
Pontus Backlund

Reputation: 1017

Firstly you shouldn't use getApplicationContext as context. It should be like this:

    Intent j = new Intent(Firstclass.this, Secondclass.class);
    startActivity(j);

Don't forget to declare the Activity in your manifest and return true at the end. Hope this helps

Upvotes: 0

rachit
rachit

Reputation: 1996

Declare SettingsActivity in manifest. also use MainActivity.this instead of getApplicationContext(). this should help.

Upvotes: 1

Mukesh Kumar Singh
Mukesh Kumar Singh

Reputation: 4522

Replace this line

return super.onOptionsItemSelected(item);

with this one

return true;

Upvotes: 3

Siddharth_Vyas
Siddharth_Vyas

Reputation: 10100

Add SettingsActivity in your manifest file.

<activity android:name="yourmainpackagename.SettingsActivity " />

Hope this helps.

Upvotes: 1

Related Questions