D.Y
D.Y

Reputation: 55

How to go another page when I press settings button on action bar?

I face a problem when I try press the settings icon on action bar and go to settings page in my android project.

Here is the code:

public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.settings) {
            startActivity(new Intent(ProfileFragment.this, SettingsActivity.class));
        }

        return super.onOptionsItemSelected(item);
}

The error is in ProfileFragment.this, SettingsActivity.class line. There is no fatal error but there is a red line under the code.

How can I solve this?

Thanks for answers.

Upvotes: 0

Views: 176

Answers (3)

Vishwesh Jainkuniya
Vishwesh Jainkuniya

Reputation: 2839

Use this

    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == R.id.settings) {
            startActivity(new Intent(getContext(), SettingsActivity.class));
        }

        return super.onOptionsItemSelected(item);
    }

Upvotes: 2

J.D.
J.D.

Reputation: 1411

Try This

 public boolean onOptionsItemSelected(MenuItem item) {

            if(item.getItemId() == R.id.settings) {
                startActivity(new Intent(getActivity().getApplicationContext(), SettingsActivity.class));
            }

            return super.onOptionsItemSelected(item);
    }

Upvotes: 0

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Change this:

startActivity(new Intent(ProfileFragment.this, SettingsActivity.class));

to :

startActivity(new Intent(getActivity(), SettingsActivity.class));

Upvotes: 0

Related Questions