-1

I have a problem when I insert sharedpreferences inside my fragment, the problem is the context: this.

public class SettingsFragment extends Fragment {


@BindView(R.id.tvResultNama)
TextView tvResultNama;

SharedPrefManager sharedPrefManager;

public SettingsFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_settings, container, false);
    ButterKnife.bind(this);
    SharedPrefManager sharedPrefManager = new SharedPrefManager(this);

    tvResultNama.setText(sharedPrefManager.getSPNama());
    }

}
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69

2 Answers2

-1

You have to pass activity context and put everything before the return.

SharedPrefManager sharedPrefManager = new SharedPrefManager(getActivity());
SpiderGod607
  • 168
  • 1
  • 5
-1

Do this in your onviewcreated or you could crash on some devices due to a null pointer exception

SharedPrefManager sharedPrefManager = new SharedPrefManager(getActivity());