0

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.

D.Y
  • 55
  • 9

3 Answers3

2

Use this

    public boolean onOptionsItemSelected(MenuItem item) {

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

        return super.onOptionsItemSelected(item);
    }
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35
0

Change this:

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

to :

startActivity(new Intent(getActivity(), SettingsActivity.class));
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
0

Try This

 public boolean onOptionsItemSelected(MenuItem item) {

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

            return super.onOptionsItemSelected(item);
    }
J.D.
  • 1,401
  • 1
  • 12
  • 21