2

I have App on Google play, on most devices it work just fine but seems like it on some Samsung(SM-G531H,SM-G530BT), HUAWEI, Lenovo devices it crashes when getSharedPreferences calls.

I take a reference to context from onAttach(Context context) in fragment method (i am not calling getSharedPreferences before onAttach)

@Override
    public void onAttach(Context context) {
        super.onAttach(context);

        mContext = context;
        //init firebase analytics
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(context);
    }

I have sharedPreference helper class with constructor

 public UserStats(Context mContext) {

    CRASH HERE

        sharedPreferences = mContext.getSharedPreferences(PREF_USER,Context.MODE_PRIVATE); 

        }

This is the method in fragment that invoke sharedPreference

private void setGoal(int calories){
        new UserStats(mContext).setNewGoal(calories);

        tvGoal.setText("Goal\n" + calories + " Cal");
    }

Any idea why it happen? and how to avoid it? (i cannot debug it i receive crashes in Firebase console)

Pavel Poley
  • 5,307
  • 4
  • 35
  • 66
  • Post all the relevant code please. – earthw0rmjim Jul 10 '17 at 07:38
  • Hey, I'm seeing inconsistencies in `onAtttach()` calls as mentioned [here](https://stackoverflow.com/questions/32077086/android-onattachcontext-not-called-for-api-23). Did you check that out? I'd say look for alternatives. – Shaishav Jul 10 '17 at 07:54
  • interesting, but i used getActivity() before and was the same result – Pavel Poley Jul 10 '17 at 08:04
  • Where are you calling that `setGoal()` method? If it's as a result of some async operation, the fragment might already be detached from its activity and the activity might be gone and all you're left with is a stale context reference. – laalto Jul 10 '17 at 08:13
  • Where do you call `setGoal()` from? Also a minor suggestion, in your constructor for `UserStats`, don't precede the parameter name with `m`. Thats against the [code-style-guide](https://source.android.com/source/code-style). – Shaishav Jul 10 '17 at 08:13
  • setGoal() called in onViewCreated() in fragment (there is no click or long delay that call this method) – Pavel Poley Jul 10 '17 at 08:17
  • `mContext` is not a useful variable, you should call `getContext()` instead which already keeps track of the fragment's current context. If you still get a crash please provide the full stack trace. – BladeCoder Jul 13 '17 at 12:29
  • Ok, i will try to change to getContext, but Why mContext is not usefull? Also Why getActivity not usefull? – Pavel Poley Jul 13 '17 at 12:50
  • Note that if in `setGoal()` you cannot use `getContext()`, then maybe `tvGoal` member will be not initialized, too. – Alex Cohn Jul 16 '17 at 10:47
  • Isn't `onAttach()` deprecated? – zerobandwidth Jul 19 '17 at 23:26

3 Answers3

2

use getApplicationContext() always if available

Rasoul Miri
  • 11,234
  • 1
  • 68
  • 78
0

Use getActivity() in replace of mContext. It does not find fragment's referrence. So , app is crashed.

Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

If you are in activity use getApplicationContext() .

If you are in fragment use getContext() .

If you are in adapter class use context and add Context parameter of adapter's constructor.