简体   繁体   中英

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...

Replace this line

return super.onOptionsItemSelected(item);

with this one

return true;

Add SettingsActivity in your manifest file.

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

Hope this helps.

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

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

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